Android:按钮不显示在布局中

标签 android layout position android-linearlayout android-button

我很困惑为什么当我使用这个布局时名为“btnAdd”的按钮没有显示。我已经尝试了 LinearLayout 和当前的 RelativeLayout,但它在两者中都是不可见的。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:padding="5dp">
  <RelativeLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
      <TextView
        android:id="@+id/lblName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:text="@string/category" />
      <AutoCompleteTextView 
        android:id="@+id/txtName"
        android:layout_marginRight="5dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/lblName"
        android:textColor="#000000"
        android:singleLine="true"/>
      <Button 
         android:id="@+id/btnAdd"
         android:layout_width="100dip"
         android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
         android:layout_toRightOf="@id/txtName"
         android:text="@string/add"/>
  </RelativeLayout>
  <TextView  
        android:id="@+id/android:empty"
        android:layout_marginTop="4dp"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/current_categories"
        />
  <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_marginTop="2dp">
    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>
    <TextView  
        android:id="@+id/android:empty"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/recipe_uncategorized"
        />
  </LinearLayout>  
</LinearLayout>

最佳答案

我现在在手机上,所以我无法测试这个,但我认为你做错了以下几点:

由于你在txtName上使用了fill_parent作为宽度,而txtName是在btnAdd之前添加的,所以btnAdd就没有剩余的宽度了。我认为切换添加两个元素的顺序应该就足够了(执行此操作时,您可能还必须对 layout_toRightOf/LeftOf 进行一些调整)。

更新:
我查了一下,确实如我所说:

  • 在文本字段前添加按钮
  • 添加文本字段时使用 layout_toLeftOf。

最终结果应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:padding="5dp">
  <RelativeLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
      <TextView
        android:id="@+id/lblName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:text="@string/category" />
      <Button 
         android:id="@+id/btnAdd"
         android:layout_width="100dip"
         android:layout_height="wrap_content"
         android:layout_below="@id/lblName"
         android:layout_alignParentRight="true"
         android:text="@string/add"/>
      <AutoCompleteTextView 
        android:id="@+id/txtName"
        android:layout_marginRight="5dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/lblName"
        android:layout_toLeftOf="@id/btnAdd"
        android:textColor="#000000"
        android:singleLine="true"/>
  </RelativeLayout>
  <TextView  
        android:id="@+id/android:empty"
        android:layout_marginTop="4dp"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/current_categories"
        />
  <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_marginTop="2dp">
    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>
    <TextView  
        android:id="@+id/android:empty"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/recipe_uncategorized"
        />
  </LinearLayout>  
</LinearLayout>

关于Android:按钮不显示在布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4574143/

相关文章:

html - 制作一个 CSS 按钮 : positioning the text

java - 警告/错误含义 - GbaRequest - GbaRequest : Constructor Called 222 userAgent Apache-HttpClient/UNAVAILABLE

php - 使用php将mp4文件上传到数据库

java - 绑定(bind) BorderPane 和框架高度和宽度

android - galaxy s4 和所有高清手机?内存不足错误膨胀布局

ios - SKLabelNode 显示在 iPhone 模拟但不是 iPad

java - 难以掌握 Dagger 基础知识

java - Android中大HashMap会导致内存不足异常吗?

html - 如何用HTML + CSS实现如下布局?

HTML 布局 : Prevent Fixed Footer from Covering Content