Android, как добавить Linearlayout в другой LinearLayout из кода

1

У меня есть LinearLayout в main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/mainLayout" >

</LinearLayout>

Я сделал еще один файл XML, называемый item_box.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/item_bg"
    android:gravity="right" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginRight="20dp"
        android:text="@string/item1"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginRight="20dp"
        android:gravity="right"
        android:text="@string/number1"
        android:textColor="@color/number_bg"
        android:textSize="30dp" />

</LinearLayout>

В основном, что я хочу сделать из кода (программно), добавьте пару item_box.xml в main.xml. Как я могу это сделать?

  • 0
    Чего вы ожидаете? Вы не добавили никаких представлений в item_box , поэтому он не будет виден.
  • 0
    Но я продолжаю получать исключение Nullpointer, и представление не отображается на моем устройстве, когда я играю на нем. Я сделал включение под названием "item_box.xml" и хочу добавить его в основной макет.
Показать ещё 4 комментария
Теги:
android-linearlayout

4 ответа

4
Лучший ответ
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);
mainLayout.addView(itemBox);
  • 1
    Лучший ответ. Золотой слот. Как;)
2

попробуйте этот linearlayout внутри другого linearlayout добавить id в linearlayout layout1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/firstlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

</LinearLayout>

в вызове oncreate()

LinearLayout firstlayout=(LinearLayout)findviewbyId(R.id.firstlayout);
LinearLayout secondlayoout=(LinearLayout)this.getLayoutInflater().inflate(R.layout.layout2,null); //inflating view from xml 
TextView btn1=(TextView)secondlayoout.findviewbyId(R.id.button1);
btn1.settext("TEST");
firstlayout.addview(secondlayoout);
1

Используйте LayoutInflater:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);
1

Вы получаете Nullpointer, потому что linearLayout1 не находится в mainLayout. Сначала вам необходимо раздуть ваш взгляд, а затем добавить его. Вы должны прочитать этот вопрос, я думаю, это поможет вам.

Ещё вопросы

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