Java textView拒绝以LinearLayout的儿子为中心
我尝试将Java textView拒绝以LinearLayout的儿子为中心,java,android,xml,layout,android-linearlayout,Java,Android,Xml,Layout,Android Linearlayout,我尝试将刷新符号和文本居中 但是文本是向右对齐的 为什么呢 XML 屏幕: 由于TextView的宽度设置为fill\u parent(改为使用match\u parent),因此将TextView的重力设置为中心将解决此问题。试试这个: <LinearLayout android:id="@+id/layout_action" android:layout_width="80dp" android:layout_height="fill_parent"
刷新
符号和文本居中
但是文本是向右对齐的
为什么呢
XML
屏幕:
由于
TextView的宽度设置为fill\u parent
(改为使用match\u parent
),因此将TextView的重力设置为中心将解决此问题。试试这个:
<LinearLayout
android:id="@+id/layout_action"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center" <-- Added
android:orientation="vertical"
android:layout_weight=".2" >
<ImageView
android:id="@+id/image_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/deletelight1" />
<TextView
android:id="@+id/h"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="Refresh"
android:gravity="center"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
由于TextView的
宽度设置为fill\u parent
(改用match\u parent
),将TextView的
重力设置为center
将解决此问题。试试这个:
<LinearLayout
android:id="@+id/layout_action"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center" <-- Added
android:orientation="vertical"
android:layout_weight=".2" >
<ImageView
android:id="@+id/image_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/deletelight1" />
<TextView
android:id="@+id/h"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:text="Refresh"
android:gravity="center"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
将android:gravity=“center”
添加到您的TextView
将android:gravity=“center”
添加到您的TextView
这是因为您已将TextView
宽度设置为fill\u parent
。TextView
现在是布局的全宽,因此从技术上讲它是居中的。将其更改为wrap\u content
或将TextView
重力设置为center
。这是因为您已将TextView
宽度设置为fill\u parent
。TextView
现在是布局的全宽,因此从技术上讲它是居中的。将其更改为wrap\u content
或将TextView
重力设置为center
。他们只需在文本视图中添加填充物即可。
或者以重力为中心。会好的。。。!!
确保你设置了重力,而不是布局重力,只需在文本视图中添加填充。
或者以重力为中心。会好的。。。!!
确保你设置了重力而不是布局重力请重新格式化XML,这是不可能读取的。我看不出问题出在哪里,但对于这种布局,每次我都会使用RelativeLayout。我只是觉得除了简单的“线性”布局外,它更适合任何东西。您的图像视图
居中,因为您已将其宽度
设置为包裹内容
,并且将父级的重力
设置为居中。请重新格式化XML,这是不可能读取的。我看不出问题所在,但对于这种布局,每次我都会和你一起去。我只是觉得除了简单的“线性”布局之外,它更适合任何布局。你的ImageView
居中,因为你已经将其width
设置为wrap\u content
并且它的父级gravity
设置为居中。那么为什么图像居中呢?我知道android:gravity属性应该出现在父布局中android:Layou gravity
是给儿子的。但是我已经有了android:gravity
你的文本视图的宽度与父母的宽度匹配。这就是为什么在父视图中声明的水平重力对该视图没有意义。在这种情况下,文本视图中的文本需要单独居中。如何正确管理权重?我尝试更改为包装内容,但它变得太对了。您可以更改文本视图的宽度以包装内容。它应该可以工作,因为你的父母的水平重力在“中心”这就是为什么在父母声明的水平重力对这个视图没有意义。
但是我知道一个儿子可以持有android:layou gravity
而不是android:gravity
。所以我希望你告诉我在我的textView
no中添加android:layou gravity
?那为什么图像居中呢?我知道android:gravity属性应该出现在父布局中android:Layou gravity
是给儿子的。但是我已经有了android:gravity
你的文本视图的宽度与父母的宽度匹配。这就是为什么在父视图中声明的水平重力对该视图没有意义。在这种情况下,文本视图中的文本需要单独居中。如何正确管理权重?我尝试更改为包装内容,但它变得太对了。您可以更改文本视图的宽度以包装内容。它应该可以工作,因为你的父母的水平重力在“中心”这就是为什么在父母声明的水平重力对这个视图没有意义。
但是我知道一个儿子可以持有android:layou gravity
而不是android:gravity
。所以我希望你告诉我在我的textView
中添加android:layou gravity
否?@Simon在上面添加了一些解释。有什么进展?匹配父项或dp中的特定宽度?布局重量与高度和宽度有关?如果其中一个在dp中具有特定的大小,则定义相同的属性。因此,这里没有定义优先级<代码>布局(重量)
使用比率,而不是高度
和宽度
(取决于方向)。我认为,如果您明确指定其中一个组件的高度(或宽度),将优先使用该设置。其余的空间将根据布局权重分布在其他组件之间。@EladBenda请参见上面的编辑1。希望它能为您澄清一些事情。@vikram(改用match\u parent)
-不建议这样做:@Simon在上面添加了一些解释。有什么先例?匹配父项或dp中的特定宽度?布局重量与高度和宽度有关?如果其中一个在dp中具有特定的大小,则定义相同的属性。因此,这里没有定义优先级<代码>布局(重量)
使用比率,而不是高度
和宽度
(取决于方向)。我认为,如果您明确指定其中一个组件的高度(或宽度),将优先使用该设置。其余的空间将根据布局权重分布在其他组件之间。@EladBenda请参见上面的编辑1。希望它能为您澄清一些事情。@vikram(改用match\u parent)
-无记录
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_action"
android:layout_width="80dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/image_action"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/widget_bt_drive_disabled" />
<TextView
android:id="@+id/h1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="6dp"
android:text="Refresh 50"
android:textColor="@color/disabled_white"
android:textSize="15sp"
android:textStyle="bold"
android:layout_weight="50" />
<TextView
android:id="@+id/h2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="6dp"
android:text="Refresh 25"
android:textColor="@color/disabled_white"
android:textSize="15sp"
android:layout_weight="25"
android:textStyle="bold" />
<TextView
android:id="@+id/h3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="6dp"
android:text="Refresh 10"
android:textColor="@color/disabled_white"
android:textSize="15sp"
android:layout_weight="10"
android:textStyle="bold" />
<TextView
android:id="@+id/h4"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="6dp"
android:text="Refresh 15"
android:textColor="@color/disabled_white"
android:textSize="15sp"
android:textStyle="bold"
android:layout_weight="15" />
</LinearLayout>