Android: кнопка не отображается в макете

1

Я озадачен тем, почему кнопка, называемая "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>
Теги:
position
layout
android-button
android-linearlayout

2 ответа

1
Лучший ответ

Сейчас я на мобильном телефоне, поэтому я не могу проверить это, но я думаю, что вы делаете следующее неправильно:

Поскольку вы используете fill_parent как width в txtName, а txtName добавляется до btnAdd, для btnAdd нет ширины. Я думаю, что этого должно быть достаточно, чтобы переключить порядок, в котором вы добавляете два элемента (вам также придется выполнить некоторую настройку layout_toRightOf/LeftOf, когда вы это сделаете).

Update:
Я проверил, и это было правильно, как я сказал:

  • Добавить кнопку перед текстовым полем
  • При добавлении текстового поля используйте 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>
  • 0
    Спасибо, сработало отлично.
0

Это потому, что ваш AutoCompleteTextView имеет fill_parent, который занимает весь экран, например. дайте layout_width = "100dip" для AutoCompleteTextView и появится ваша кнопка. Я предлагаю вам использовать layout_weights, чтобы предоставить пространство для ваших кнопок и видов.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню