【问题标题】:Android: Button does not show in layoutAndroid:按钮不显示在布局中
【发布时间】:2011-01-01 13:09:06
【问题描述】:

我很困惑为什么当我使用这个布局时没有显示名为“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>

【问题讨论】:

    标签: android layout position android-linearlayout android-button


    【解决方案1】:

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

    由于您在 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>
    

    【讨论】:

      【解决方案2】:

      那是因为您的 AutoCompleteTextView 有 fill_parent ,它占据了整个屏幕,例如为 AutoCompleteTextView 提供 layout_width = "100dip" ,您的按钮就会出现。我建议您使用 layout_weights 为按钮和视图留出空间。

      【讨论】:

        猜你喜欢
        • 2014-10-09
        • 1970-01-01
        • 1970-01-01
        • 2018-10-18
        • 2017-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-01
        相关资源
        最近更新 更多