【问题标题】:add LinearLayout within another LinearLayout through java通过java在另一个LinearLayout中添加LinearLayout
【发布时间】:2017-03-17 05:11:21
【问题描述】:

我有一个布局,它充当一种具有两个线性布局的表格。主要的线性布局具有垂直方向,而其中的一个具有水平方向。代码如下...

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>

我想在此布局中添加新条目并将其视为表格。我要添加的条目是第二个线性布局内的三个文本视图。由于我是通过 php 数据库获取数据,因此我想通过 java 来实现这一点,知道如何做到这一点吗?

【问题讨论】:

  • 如果你想制作一个类似视图的表格,那么 RecyclerView 或 ListView 将是一个更好的主意。
  • 其实这不是我的代码,我只是为朋友做的。他喜欢这样……

标签: android android-layout android-linearlayout


【解决方案1】:

通过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>

【讨论】:

  • 您能进一步解释一下吗?
  • 第二个布局在运行时膨胀到第一个布局中。再次阅读我的答案。最美好的祝愿:)
  • 不,我问的是如果我只是编辑文本和图像视图并在其中添加视图,它会工作吗?
  • 等等...我也会发布 xml
  • first_layout 是 mainActivity 的一部分,您可以通过 findViewById 访问它。然后使用上面的代码对 second_layout.xml 进行膨胀并添加。如果您想要 10 行,请将相同的布局膨胀 10 次并将其添加到容器布局中。欢呼(Y)
【解决方案2】:

更改您的 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”个项目。他们的字体、背景、重力等等一切。希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 2012-11-23
    • 2016-09-19
    • 1970-01-01
    相关资源
    最近更新 更多