【问题标题】:Dynamic TextViews wrapping to next row/line in Linear layout动态TextViews在线性布局中换行到下一行/行
【发布时间】:2012-03-18 07:31:05
【问题描述】:

我正在尝试构建一个 UI,其中我有一个已在 XML 中定义的线性布局。

根据用户的输入,我需要在这个线性布局中添加一些 TextView。我能够做到这一点。

我的实际问题是,当我有更多文本视图时,它们彼此相邻堆叠,并且一些文本视图文本被隐藏或垂直拉伸,如下图所示。

我想使用线性布局的整个宽度,如果文本视图无法容纳在这一行中,则应将其放入新行或第一个文本视图下方。我希望显示为如下。

以下是我在 XML 中的线性布局配置:

    <RelativeLayout
        android:id="@+id/RL1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:paddingBottom="5dip"
        android:paddingTop="5dip" >

        <TextView
            android:id="@+id/text1"
            android:layout_width="85dip"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:gravity="left"
            android:text="Lable 1"
            android:textColor="#999999" />

        <LinearLayout
            android:id="@+id/LL1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/button1"
            android:layout_toRightOf="@+id/text1"
            android:gravity="left|center_vertical"
            android:orientation="horizontal"
            android:paddingLeft="5dip"
            android:paddingRight="5dip"
            android:paddingTop="3dip" >
        </LinearLayout>

        <Button
            android:id="@+id/button1"
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="2dip"
            android:background="@drawable/plus"
            android:clickable="true"
            android:focusable="true"
            android:paddingTop="5dip" />
    </RelativeLayout>

任何人都可以帮助我如何在java代码中重新对齐。

【问题讨论】:

  • 在您的 xml 中,您只有一个文本视图和一个按钮,只有右边...
  • 其余的文本视图是动态添加的......来自java代码
  • 这个关于 ConstraintLayout Flow 的答案可能适用...stackoverflow.com/questions/6996837/…

标签: android android-layout alignment android-linearlayout textview


【解决方案1】:

我想建议您如何为线性布局中的所有视图提供相同的大小,您可以通过在 XML 文件中使用 weight 属性来做到这一点,即 android:weight 用于该特定视图以及何时使用它属性你应该给 width=0dp 或 0dip。我认为这会很容易地解决你的问题。

请我建议您首先在以下链接中充分了解如何使用此属性:-Weight property with an example

【讨论】:

  • 我试过它有一个缺点,你使用 4 行来显示 4 个项目。我可以在 2 行中完成它们,如上所示。
【解决方案2】:

请看:

How to wrap Image buttons in a horizontal linear layout?

How can I do something like a FlowLayout in Android?

您也可以在 github 上搜索 FlowLayout.java

另一种方法如下所示: Android - multi-line linear layout

此外,还有一个类可以将图像添加到 TextView 中: https://stackoverflow.com/a/21250752/755804 这与一般情况下的包装视图不同,但有时可以完成这项工作。

【讨论】:

    【解决方案3】:

    我是韩国人。 所以我英语说得不好,但我会帮你的。

    首先,创建带有四个文本框的“item.xml”。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/set_checkbox"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text2"
        android:text="0"
        android:layout_weight="1"
        android:gravity="center"/>
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text3"
        android:text="0"
        android:layout_weight="4"
        android:gravity="center"/>
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text4"
        android:text="0"
        android:layout_weight="4"
        android:gravity="center"/>
    </LinearLayout>
    

    其次,创建“main.xml”,其中“item.xml”将动态生成。 'orientation' 有助于一次创建一条线。

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

    最后,您可以使用下面的代码每行创建四个 TextView。

            LinearLayout layout = (LinearLayout)findViewById(R.id.mainxml); 
            LinearLayout item = (LinearLayout)layout.inflate(this.getContext(), R.layout.itemxml, null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
            item.setLayoutParams(params);
    
    
            CheckBox cb = item.findViewById(R.id.set_checkbox);
            TextView text2 = item.findViewById(R.id.text2);
            TextView text3 = item.findViewById(R.id.text3);
            TextView text4 = item.findViewById(R.id.text4);
    
            cb.setChecked(true);
            text2.setText("text2");
            text3.setText("text3");
            text4.setText("text3");
    
            layout.addView(item);
    

    第 10 个循环如下图所示。 enter image description here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多