Java 为什么设置为左/右的布局\重力在水平线性布局中没有影响?
我正在尝试设置两个Java 为什么设置为左/右的布局\重力在水平线性布局中没有影响?,java,android,android-linearlayout,textview,Java,Android,Android Linearlayout,Textview,我正在尝试设置两个TextViews,一个在父视图的左侧,另一个在右侧。 我原以为layou\u gravity就足够了。 但它不起作用。示例代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="300dp"
TextView
s,一个在父视图的左侧,另一个在右侧。我原以为
layou\u gravity
就足够了。但它不起作用。示例代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LEFT"
android:textSize="14sp"
android:layout_gravity="left"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RIGHT"
android:textSize="14sp"
android:layout_gravity="right"
/>
</LinearLayout>
</LinearLayout>
我做错了什么?试试这个:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="LEFT"
android:layout_weight=".5"
android:layout_gravity="start"
android:gravity="start"
android:textSize="14sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="RIGHT"
android:layout_weight=".5"
android:layout_gravity="end"
android:gravity="end"
android:textSize="14sp"/>
</LinearLayout>
试试这个:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="LEFT"
android:layout_weight=".5"
android:layout_gravity="start"
android:gravity="start"
android:textSize="14sp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="RIGHT"
android:layout_weight=".5"
android:layout_gravity="end"
android:gravity="end"
android:textSize="14sp"/>
</LinearLayout>
LinearLayout
在布局方向上不使用layout\u gravity
。您可以在水平线性布局中使用layou gravity
来控制它们垂直放置的方式:顶部、中心或底部
您可以将LinearLayout
更改为垂直,然后左/右重力将起作用,但第二个TextView
将放置在比第一个更低的位置。我想这不是你想要的
在我看来,您似乎对LinearLayout
提供的布局逻辑不感兴趣(即依次放置子项)。您只需要一个左对齐,一个右对齐。如果您将文本视图
放在框架布局
中,则可以使用重力来实现该效果。线性布局
不使用布局重力
作为布局方向。您可以在水平线性布局中使用layou gravity
来控制它们垂直放置的方式:顶部、中心或底部
您可以将LinearLayout
更改为垂直,然后左/右重力将起作用,但第二个TextView
将放置在比第一个更低的位置。我想这不是你想要的
在我看来,您似乎对LinearLayout
提供的布局逻辑不感兴趣(即依次放置子项)。您只需要一个左对齐,一个右对齐。如果您将TextView
s放在FrameLayout
中,则可以使用重力来实现此效果。请尝试以下方法:
编辑Vincible的答案
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="LEFT"
android:textSize="14sp"
android:layout_gravity="left"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="RIGHT"
android:textSize="14sp"
android:layout_gravity="right"
/>
</LinearLayout>
尝试以下方法:
编辑Vincible的答案
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="LEFT"
android:textSize="14sp"
android:layout_gravity="left"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="RIGHT"
android:textSize="14sp"
android:layout_gravity="right"
/>
</LinearLayout>
这似乎可行,但为什么我需要定义重力和重量?我认为layout\u gravity
设置了父级中的位置<代码>重力
仅用于文本(内容)。你能解释一下吗?你想让这两篇文章平分一半;这就是为什么我们需要在这里使用重量。我们还需要将宽度设置为零(0),因为方向是水平的。权重将每个项目放在其位置(右侧和左侧)。重力将项目定位在其所需位置。我认为layou Gravity
right将TextView
定位在父视图的右侧。似乎我误解了什么。当你使用权重时,你创造了两个相等的边。现在,在每一侧,可以使用“重力”属性再次定位项目(起点、中心或终点)。每一方都有相应的定位能力。我了解砝码是如何工作的,我只想把它作为一种解决办法或“肮脏的黑客”。我认为只有layout\u gravity
就足够了,因为它是关于在父视图中定位视图的,这似乎是可行的,但是为什么我需要定义重力和重量呢?我认为layout\u gravity
设置了父级中的位置<代码>重力
仅用于文本(内容)。你能解释一下吗?你想让这两篇文章平分一半;这就是为什么我们需要在这里使用重量。我们还需要将宽度设置为零(0),因为方向是水平的。权重将每个项目放在其位置(右侧和左侧)。重力将项目定位在其所需位置。我认为layou Gravity
right将TextView
定位在父视图的右侧。似乎我误解了什么。当你使用权重时,你创造了两个相等的边。现在,在每一侧,可以使用“重力”属性再次定位项目(起点、中心或终点)。每一方都有相应的定位能力。我了解砝码是如何工作的,我只想把它作为一种解决办法或“肮脏的黑客”。我认为只有layout\u gravity
就足够了,因为它是关于在父视图中定位视图的,所以你的意思是它忽略了它吗?我将不得不阅读更多关于这方面的内容@Marcin,请提供这方面的文档?@Jim我扩展了我的答案,来解释LinearLayout
如何使用layout\u gravity
。@Eenvincible to be fair文档并没有真正涵盖它。在示例应用程序中尝试,设置父布局和布局值的不同配置。或者阅读源代码!你的意思是它忽略了它?我要读更多关于这方面的内容@Marcin,请提供这方面的文档?@Jim我扩展了我的答案,来解释LinearLayout
如何使用layout\u gravity
。@Eenvincible to be fair文档并没有真正涵盖它。在示例应用程序中尝试,设置父布局和布局值的不同配置。或者阅读源代码!