通过java在另一个LinearLayout中添加LinearLayout

时间:2017-03-16 18:43:42

标签: android android-layout android-linearlayout

我有一个布局,作为一种具有两个线性布局的表格。主线性布局具有垂直方向,并且该线性布局内部具有水平方向。代码如下......

XML文件......

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/view_below_linear_layout_three_team_spuck"
    android:background="@drawable/cell_shape"
    android:id="@+id/linear_layout_four_team_spuck"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="45dp">

        <TextView
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:text="1"
            android:id="@+id/serial_number_team_spuck"
            android:gravity="center"
            android:background="@drawable/cell_shape"
            android:textSize="20dp"
            />

        <ImageView
            android:layout_width="50dp"
            android:layout_height="35dp"
            android:layout_gravity="center"
            android:id="@+id/the_smallperson_2_team_spuck"
            android:src="@drawable/the_smallperson"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/serial_number_team_spuck"
            android:layout_toEndOf="@+id/serial_number_team_spuck" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="#B2B2B2"
            android:textSize="12dp"
            android:gravity="center"
            android:id="@+id/spuck_table_name"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/the_smallperson_2_team_spuck"
            android:layout_toEndOf="@+id/the_smallperson_2_team_spuck"
            android:layout_marginLeft="14dp"
            android:layout_marginStart="14dp" />



        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="#B2B2B2"
            android:textSize="12dp"
            android:gravity="center"
            android:id="@+id/spuck_table_speed"
            android:layout_marginLeft="30dp"
            android:layout_marginStart="30dp"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/spuck_table_name"
            android:layout_toEndOf="@+id/spuck_table_name" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="#B2B2B2"
            android:textSize="12dp"
            android:gravity="center"
            android:id="@+id/spuck_table_points"
            android:layout_marginLeft="29dp"
            android:layout_marginStart="39dp"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/spuck_table_speed"
            android:layout_toEndOf="@+id/spuck_table_speed" />

    </LinearLayout>
</LinearLayout>

我想在此布局中添加新条目,并将其视为表格。我要添加的条目是第二个linearlayout中的三个textview。由于我通过php数据库获取数据,我想通过java实现这一点,任何关于如何做到这一点的想法?

2 个答案:

答案 0 :(得分:2)

  

通过java

在另一个LinearLayout中添加LinearLayout

你可以像这样膨胀布局

  LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
 View inflatedLayout= inflater.inflate(R.layout.second_layout, null, false);
 firstLayout.addView(inflatedLayout);

first_layout.xml

   <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/view_below_linear_layout_three_team_spuck"
    android:background="@drawable/cell_shape"
    android:id="@+id/linear_layout_four_team_spuck"
    android:orientation="vertical">

</LinearLayout>

<强> second_layout.xml

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="45dp">

    <TextView
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:text="1"
        android:id="@+id/serial_number_team_spuck"
        android:gravity="center"
        android:background="@drawable/cell_shape"
        android:textSize="20dp"
        />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="35dp"
        android:layout_gravity="center"
        android:id="@+id/the_smallperson_2_team_spuck"
        android:src="@drawable/the_smallperson"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/serial_number_team_spuck"
        android:layout_toEndOf="@+id/serial_number_team_spuck" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="#B2B2B2"
        android:textSize="12dp"
        android:gravity="center"
        android:id="@+id/spuck_table_name"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/the_smallperson_2_team_spuck"
        android:layout_toEndOf="@+id/the_smallperson_2_team_spuck"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp" />



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="#B2B2B2"
        android:textSize="12dp"
        android:gravity="center"
        android:id="@+id/spuck_table_speed"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/spuck_table_name"
        android:layout_toEndOf="@+id/spuck_table_name" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="#B2B2B2"
        android:textSize="12dp"
        android:gravity="center"
        android:id="@+id/spuck_table_points"
        android:layout_marginLeft="29dp"
        android:layout_marginStart="39dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/spuck_table_speed"
        android:layout_toEndOf="@+id/spuck_table_speed" />

</LinearLayout>

答案 1 :(得分:1)

更改你的xml saim,你应该使用一个简单的TableLayout来做这些事情。

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/view_below_linear_layout_three_team_spuck"
    android:id="@+id/the_maintable_team_spuck"
    >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/the_maintable_row_team_spuck">

    </TableRow>
</TableLayout>

我没有在行中添加任何项目,因为可以通过java添加'n'个项目。他们的字体,背景,重力等一切。希望这会有所帮助:)