Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java textView拒绝以LinearLayout的儿子为中心_Java_Android_Xml_Layout_Android Linearlayout - Fatal编程技术网

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>