Android 如何使水平线性布局子包装其内容?

Android 如何使水平线性布局子包装其内容?,android,android-layout,android-linearlayout,android-xml,Android,Android Layout,Android Linearlayout,Android Xml,正如标题所示,在我的例子中,子视图是一个包含一些内容的TextView。我希望每行一个 因此,将layout\u width添加到0dp并将layout\u weight添加到1不起作用,我假设因为它是行中唯一的一个,所以1是最高的宽度。。。不过我不确定 这是xml: <LinearLayout android:id="@+id/tagsVerticalLineup" android:layout_width="0dp" androi

正如标题所示,在我的例子中,子视图是一个包含一些内容的
TextView
。我希望每行一个

因此,将
layout\u width
添加到
0dp
并将
layout\u weight
添加到
1
不起作用,我假设因为它是行中唯一的一个,所以
1
是最高的宽度。。。不过我不确定

这是xml:

    <LinearLayout
        android:id="@+id/tagsVerticalLineup"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:gravity="center_horizontal">

        <TextView 
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
         />

        <TextView 
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
         />

        <TextView 
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
         />

    </LinearLayout>
文本视图
应答标签

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/SelectedTagAnswer" />
注意

将简单的
文本视图插入到使用相同样式的xml时,

它的工作原理类似于@Ajil O answer。充气过程中有些东西把它弄乱了。

只需在
android:layout\u width
中使用
wrap\u content
参数,您就可以使用
0dp
了:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/tagsVerticalLineup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

</LinearLayout>

使
线性布局
宽度与
父项
匹配,高度与
包裹内容

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

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />

</LinearLayout>

最好使用ContrinstLayout,并且您可以将任何事件操纵到您想要的维度上

最终找到了一个解决方案,因此Android不会在显示内容后刷新视图的布局

正如在这个答案中所发现的那样

所以我的问题是膨胀视图,然后添加内容(文本)

为了解决这个问题,我再次设置了高度和宽度,如下所示:

    tag.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
现在,如果答案中的所有内容都实现了,那么它就可以工作了

希望此edge手机壳在将来对某人有用

试试这个
  • 使父布局的
    高度和宽度=匹配\u父布局
  • textView
    make width
    match\u parent
    以便您可以使用
    textalignment=center
    或使用
    gravity=center



我认为将所有文本视图的宽度更改为wrap\u内容应该可以解决您的问题。使用wrap\u content with width参数。但是它的大小会随着文本限制的增加而水平增加。Mehul Kanzariya不,我在阅读
weight
solution之前尝试过它,它也不适用于me@darthydarth我试过你的代码,现在我有三个文本视图垂直对齐,它们的大小随着长度的增加而水平增加。我发现你的问题很令人困惑;)我不明白你的问题?如果您希望文本视图只有一行,请使用maxLines=1。这是可以的,但在我的情况下,我需要
文本视图
s来包装其内容,这里它们的大小将是最长的text@darthydarth然后你有两个选项,要么给文本视图加上硬编码的宽度,文本大小增加时会下降。如果您想将其固定,请使用maxlines=“1”和硬编码宽度。您完全正确,但在这种情况下,我不能这样做,因为我以编程方式添加和删除
文本视图
s,因此约束会非常混乱quickly@darthydarth您确定正在查看最新的编辑吗?此时您将获得什么输出?您需要如何获得?获取
文本视图
s拉伸设备/线性布局的全宽和
线性布局
的高度与
文本视图
内容相同。短-全宽和压扁vertically@darthydarth在这种情况下,我不明白为什么的答案对你不起作用。在我的回答中,文本视图不会扩展到
线性布局的宽度(除非文本很长,ofc)。在这个回答中
文本视图
仍在扩展全宽。在Umair的回答中,它们都延伸到
文本视图的大小
宽度——最长的文本,这也是不好的。也许使用
线性布局没有好的解决方案,但我不能使用约束。。。。感觉hopeless@darthydarth如果你发布更详细的信息,很有可能有人能提供帮助。谢谢,我已经弄明白了,请参阅答案
<LinearLayout
    android:id="@+id/tagsVerticalLineup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     />

</LinearLayout>
<LinearLayout
    android:id="@+id/tagsVerticalLineup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
    android:background="#AAAAFF"
    android:layout_gravity="center">

    <TextView
        android:layout_margin="8dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#080"
        android:text="small text"
        android:textColor="#FFF"
        android:textSize="18sp"
        android:maxLines="1"/>

    <TextView
        android:layout_margin="8dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Slightly longer text"
        android:background="#400"
        android:textColor="#FFF"
        android:textSize="18sp"
        android:maxLines="1"/>

    <TextView
        android:layout_margin="8dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="loooooooooooooooooong text"
        android:background="#008"
        android:textColor="#FFF"
        android:textSize="18sp"
        android:maxLines="1"/>

</LinearLayout>
    tag.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
<LinearLayout
    android:id="@+id/tagsVerticalLineup"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"/>

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"     />

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"/>

</LinearLayout>