Android 线性布局:如何将视图放置在填充父视图的下方/右侧?
我想在左边有一个填充空间的文本视图,在右边加上一个右对齐的文本视图。我尝试过摆弄重力、布局重力,并使第二个TextView填充父视图,但第二个TextView总是不可见,只要第一个是填充父视图。我注意到,如果LinearLayout是垂直的,同样的问题也会发生。怎么办Android 线性布局:如何将视图放置在填充父视图的下方/右侧?,android,android-linearlayout,Android,Android Linearlayout,我想在左边有一个填充空间的文本视图,在右边加上一个右对齐的文本视图。我尝试过摆弄重力、布局重力,并使第二个TextView填充父视图,但第二个TextView总是不可见,只要第一个是填充父视图。我注意到,如果LinearLayout是垂直的,同样的问题也会发生。怎么办 <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravit
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="fill_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Left Side"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right side"
android:background="#ff606060"/>
</LinearLayout>
目标:Android 2.2使用
layout\u weight
并将layout\u width
设置为0dp
。
因此,在第一个文本视图上,权重设置为3,然后在第二个文本视图上设置为1,例如/使用
layout\u权重
,并在TextView
和两个文本视图上,将layout\u宽度
设置为0dp
。
因此,权重在第一个文本视图上设置为3,然后在第二个文本视图上设置为1,例如/为什么不使用RelativeLayout
将右对齐的文本视图设置为layout_alignParentRight,然后将另一个填充空间的文本视图设置为layout_alignParentLeft,并将layout_设置为右对齐的文本视图的左对齐,为什么不使用RelativeLayout
将右对齐的文本视图设置为layout_alignParentRight,然后将另一个填充空间的文本视图设置为layout_alignParentLeft,并将layout_设置为右对齐的文本视图的左侧。
如果不希望左视图换行,也可以在左视图中添加单线(和省略号)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Left Side"
android:layout_weight="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right side"
/>
</LinearLayout>
我想这就是你想要的。
如果不希望左视图换行,也可以在左视图中添加单线(和省略号)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Left Side"
android:layout_weight="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right side"
/>
</LinearLayout>
这是因为fill_家长正在将您最右边的文本视图推离屏幕。举重是做到这一点的方法。我必须自己做这件事。在我看来,谷歌将实现一个填充无效功能,这样,如果你有一个视图想要是静态的,另一个视图想要填充其余的,它就会这样做。这是因为填充父视图正在将你最右边的文本视图推离屏幕。举重是做到这一点的方法。我必须自己做这件事。在我看来,谷歌将实现一个填充空洞的功能,这样,如果你有一个视图希望是静态的,另一个视图希望填充其余视图,它就会这样做。谢谢。实际上,layout\u width=“0”
有一个不想要的效果(文本视图的大小不根据其内容而定),所以我只需在两个视图上设置layout\u width=“wrap\u content”
,然后在左视图上设置layout\u weight=“1”
(或任何正数)以填充空间。gravity
属性似乎没有任何作用,因此我已将其删除。谢谢。实际上,layout\u width=“0”
有一个不想要的效果(文本视图的大小不根据其内容而定),所以我只需在两个视图上设置layout\u width=“wrap\u content”
,然后在左视图上设置layout\u weight=“1”
(或任何正数)以填充空间。重力属性似乎没有任何作用,因此我将其删除。android:singleLine=“true”
似乎足够了;默认情况下,文本被省略。你是对的,我最近似乎添加了一些多余的属性。android:singleLine=“true”
似乎足够了;默认情况下,文本被省略。你是对的,我最近添加了一些多余的属性。