如何在scrollView中动态添加自定义布局到linearlayout

How to add custom layout to linear layout in scrollView dynamicaly

我想显示一个文本区域垂直滚动而图像区域水平滚动的屏幕。 我创建了两个布局:

activity_display.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ScrollView
        android:id="@+id/displaySV1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginBottom="20dp">

        <LinearLayout
            android:id="@+id/displayLL1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">
        </LinearLayout>

    </ScrollView>

    <HorizontalScrollView
        android:id="@+id/displaySV2"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginBottom="20dp">

        <LinearLayout
            android:id="@+id/displayLL2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

and custom_layout.xml : 我想在线性布局中添加自定义布局 displayLL1.

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/customFrame">

    <TextView
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/customTV1"
        android:layout_gravity="start|top"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:text="Test text"
        android:layout_marginTop="5dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/customTV2"
        android:layout_gravity="top|center_horizontal"
        android:text="Test Value"
        android:layout_marginTop="5dp" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EDIT"
        android:id="@+id/editButton"
        android:layout_gravity="right|top" />
</FrameLayout>

显示 activity 来电:

super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.displayLL2);
for(int i = 0; i < count1; i++)
            {
                LinearLayout linearLayout = (LinearLayout) findViewById(R.id.displayLL1);
                FrameLayout custLayout = (FrameLayout) findViewById(R.id.customFrame);
                TextView label = (TextView) findViewById(R.id.customTV1);
                TextView value = (TextView) findViewById(R.id.customTV2);

                label.setText("Test");
                value.setText("Test");
                linearLayout.addView(custLayout);
            }

            for (int x = 0; x < count2; x++)
            {
                final ImageView image = new ImageView(this);
                image.setBackgroundResource(R.drawable.ic_launcher);
                linearLayout2.addView(image);
            }

如果文本部分被注释掉,我可以填充图像部分。但是上面的代码没有显示在屏幕上。 知道我可能哪里出错了吗? 谢谢!

可能您遇到了 NullPointerException

因为你的activityxml中没有customTV1customTV2id,所以调用findViewById(R.id.customTV1);会return一个空值, label.setText("Test"); 将抛出 NullPointerException。

实际上调用 FrameLayout custLayout = (FrameLayout) findViewById(R.id.customFrame); 不会将您的自定义 xml 添加到 activity 中,您需要对其进行充气。

试试这个代码:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.displayLL1);
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.displayLL2);

LayoutInflater inflater = LayoutInflater.from(yourActivity.this);    

for(int i = 0; i < count1; i++){

    View custLayout= inflater.inflate(R.layout.custom_layout, null, false);
    TextView label = (TextView) custLayout.findViewById(R.id.customTV1);
    TextView value = (TextView) custLayout.findViewById(R.id.customTV2);

    label.setText("Test");
    value.setText("Test");
    linearLayout.addView(custLayout);
}

for (int x = 0; x < count2; x++){
  ImageView image = new ImageView(this);
  image.setBackgroundResource(R.drawable.ic_launcher);
  linearLayout2.addView(image);
}