Android: почему layout_marginTop работает только с тегом include? - PullRequest
1 голос
/ 14 июля 2011

У меня есть следующий шаблон в моем приложении для Android.Когда я использую android: layout_MarginTop = "20dip" работает хорошо, я вижу пространство 20dip в верхней части макета.

<LinearLayout
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
style="@style/list_buttom_single"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content"  />
<LinearLayout android:gravity="center_vertical" android:paddingLeft="10dip" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0">
    <TextView android:text="Login / Register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/title" style="@style/content_page_large_text" />
    <TextView android:text="sample text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/subtitle" android:visibility="visible" style="@style/content_page_small_text" />
</LinearLayout>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/itemCount" android:visibility="gone" style="@style/content_page_large_count_text" />
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chevron" style="@style/list_buttom_chevron" />
</LinearLayout> 

Однако, когда я пытаюсь сделать это программно, используя LayoutInflater, я теряю это пространство в 20 дип.

        View notesView = mInflater.inflate(R.layout.list_item_single, null);
    ((TextView) notesView.findViewById(R.id.title)).setText("Notes");
    ((TextView) notesView.findViewById(R.id.subtitle)).setText("102 notes");
    btnContainer.addView(notesView);

Почему я получаю два разных поведения для одного и того же действияЕдинственное отличие состоит в том, что один включает uisng xml, а другой использует java.

Большое спасибо T

1 Ответ

2 голосов
/ 14 июля 2011

Поскольку параметры макета связаны только с представлением, если вы указали родительское представление Вот почему вы должны использовать другую версию inflate():

View notesView = mInflater.inflate(R.layout.list_item_single, btnContainer, false);
...
btnContainer.addView(notesView);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...