如何在scrollView dynamicaly

时间:2016-01-16 22:59:07

标签: android android-layout scrollview

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

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>

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>

显示活动电话:

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);
            }

如果文本部分被注释掉,我可以填充图像部分。但是使用上面的代码,屏幕上不显示任何内容。 知道我可能会出错吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

可能你有一个 NullPointerException

由于您的活动xml中没有customTV1customTV2 ID,因此调用findViewById(R.id.customTV1);将返回空值,label.setText("Test");将抛出NullPointerException。

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

试试这段代码:

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);
}