基于Android中条件xml设计的文本的不同组件

时间:2016-05-18 10:35:22

标签: java android android-layout

我正在尝试在android中创建表单,如下图所示。在android中创建这种类型的表单的最佳方法是什么。如何在android xml设计中实现这种类型的表单。组件不是动态的静态组件。我需要添加所有动态组件。

可能需要在现场进行验证。

enter image description here

2 个答案:

答案 0 :(得分:0)

  

使用 GridLayout 来实现此类布局。您可以根据自己的意愿提供文本。

例如:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="4"
    android:rowCount="4" >

    <TextView
        android:text="1,1" />

    <TextView
        android:text="1,2" />

    <TextView
        android:text="1,3" />

    <TextView
        android:text="1,4" />

    <TextView
        android:text="2,1" />

    <TextView
        android:text="2,2" />

    <TextView
        android:text="2,3" />

    <TextView
        android:text="2,4" />

    <TextView
        android:text="3,1" />

    <TextView
        android:text="3,2" />

    <TextView
        android:text="3,3 longer" />

    <TextView
        android:text="3,4" />

    <TextView
        android:text="4,1" />

    <TextView
        android:text="4,2" />

    <TextView
        android:text="4,3" />

    <TextView
        android:text="4,4" />

</GridLayout>

enter image description here

答案 1 :(得分:0)

首先,您需要将带有LinearLayout的Scroll View作为其子项,给它一个id,例如mLinearLayout。使用该ID,您可以向该布局添加n个视图。

以编程方式将布局添加到线性布局的示例方法,假设您已经

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    </LinearLayout>
</ScrollView>

现在,要以编程方式将视图添加到线性布局,假设您要将TextView添加到线性布局,那么

TextView textView = new TextView(this);
textView.setText("Hello World!");
textView.setId(5);
textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
mLinearLayout.addView(textView);