开发者

Android: Button does not show in layout

I'm puzzled as to why the button called "btnAdd" doesn't show when I use this layout. I've tried both a LinearLayout and the current RelativeLayout but it's invisible in both.

<?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/lblN开发者_C百科ame"
        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>


I'm on a mobile phone now, so I can't test this, but I think you're doing the following wrong:

Since you use fill_parent as width on txtName, and txtName is added before btnAdd, there is no width left for btnAdd. I think it should be sufficient to switch the order in which you add the two elements (you might also have to do some adjustment of the layout_toRightOf/LeftOf when you do this).

Update:
I checked, and it was correct as I said:

  • Add the button before the text field
  • Use layout_toLeftOf when adding the text field.

Final result should look like this:

<?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>


That's because your AutoCompleteTextView has fill_parent , which is occupying entire screen , e.g. give layout_width = "100dip" for AutoCompleteTextView and your button will appear . I will suggest you to use layout_weights to give space for your buttons and views.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜