Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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
Android 如何对齐文本,同时忽略同级图像?_Android_Xml_Android Layout_Android Studio - Fatal编程技术网

Android 如何对齐文本,同时忽略同级图像?

Android 如何对齐文本,同时忽略同级图像?,android,xml,android-layout,android-studio,Android,Xml,Android Layout,Android Studio,考虑以下布局: <LinearLayout android:id="@+id/headerLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:contentDescription="@string/pipb

考虑以下布局:

<LinearLayout
    android:id="@+id/headerLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
         android:contentDescription="@string/pipboyImage_contentDescription"
        android:id="@+id/pipboyIcon"
        android:layout_height="40dp"
        android:layout_width="40dp"
        android:src="@drawable/pipboy"/>

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="@string/title"
        android:textAlignment="center"
        android:textSize="20sp"
        android:textStyle="bold"/>
</LinearLayout>

在这个示例中,我试图为一个大学练习重新创建一个界面。两个相关要求是,我们必须使用
线性布局
,并且必须与API 15兼容。我在复制文本的精确对齐方面遇到了困难,尽管我已经确认,我到目前为止所做的对于教师定义的精确复制所提供的界面是可以接受的,但我似乎无法找到对齐文本的正确方法,这让我感到困扰

文本需要与中心对齐。但是,此对齐似乎不确认直接放置在其左侧的图像。在本例中,文本与屏幕中心完全对齐。在我的示例中,我能得到的最好结果是在设置图像后将文本与剩余空间的中心对齐

我一直在玩android:layout_weight,最终图像的权重为1,文本的权重为4。充其量,我的布局和现在一样。我还玩过android:Layou\u gravity,但我似乎能做的就是将我的对齐方式从我想要的位置移得更远

在网上做一些研究,有人建议我使用
RelativeLayout
。这不是一个选项,因为老师坚持我们只使用
LinearLayout
。一名学生建议使用
android:paddingLeft
。这当然是一个选择,但我觉得这并不能提供完美的定位。它将简单地“模拟”我正在测试的环境中所需的布局

我还担心android:textAlignment=“center”的使用,因为这报告了对API 17的支持,我们被特别告知要使用对API 15的支持。再一次,未能修复这一轻微的偏差不会让我失去分数,所以我并不担心我没有“按照规范”正确地实现它。这就是说,该示例是“按照规范”提供的,因此我很好奇如何使用API 15支持和
LinearLayout
执行此操作


如何以忽略同级对象的方式对齐文本,仅使用
LinearLayout
并强制实施API 15兼容性?

您可以尝试此方法。它可能在以下方面发挥作用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:gravity="center"
    android:text="@string/app_name_full"
    android:textAlignment="center"
    android:textSize="20sp"
    android:textStyle="bold" />

<ImageView
    android:layout_width="40dp"
    android:layout_height="40dp" 
    android:src="@drawable/app_icon"/>

</RelativeLayout>

您可以试试这个。它可能在以下方面发挥作用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:gravity="center"
    android:text="@string/app_name_full"
    android:textAlignment="center"
    android:textSize="20sp"
    android:textStyle="bold" />

<ImageView
    android:layout_width="40dp"
    android:layout_height="40dp" 
    android:src="@drawable/app_icon"/>

</RelativeLayout>

放置一个虚拟
视图
&将其宽度和高度设置为与
图像视图
相同。试一试

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/pipboyIcon"
        android:layout_height="40dp"
        android:layout_width="40dp"
        android:src="@drawable/pipboy"/>

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:text="Exercise 2 Going mad"
        android:gravity="center"
        android:textSize="20sp"
        android:textStyle="bold"/>

    <View
        android:layout_width="40dp"
        android:layout_height="40dp"/>

</LinearLayout>
注意:这适用于小文本。如果长度很长,这将 重叠图像


放置一个虚拟的
视图
&将其宽度和高度设置为与
图像视图
相同。试一试

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/pipboyIcon"
        android:layout_height="40dp"
        android:layout_width="40dp"
        android:src="@drawable/pipboy"/>

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:text="Exercise 2 Going mad"
        android:gravity="center"
        android:textSize="20sp"
        android:textStyle="bold"/>

    <View
        android:layout_width="40dp"
        android:layout_height="40dp"/>

</LinearLayout>
注意:这适用于小文本。如果长度很长,这将 重叠图像


我能够使用张贴的备选答案实现所需的对齐,关于幻影视图以平衡图像。然而,尽管作者坚持它应该在没有任何额外重量的情况下按原样工作,但我发现我仍然需要使用重量来达到所需的对齐

这是我的最终布局,经过调整以获得所需的结果,在我随问题发布的示例图像中

<LinearLayout
    android:id="@+id/main_headerLayout"
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    >

    <ImageView
        android:id="@+id/main_pipboyIcon"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:src="@drawable/pipboy"
        />

    <TextView
        android:id="@+id/main_headerText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:text="@string/main_title"
        android:gravity="center"
        android:textSize="20dp"
        android:textStyle="bold"
        />

    <View
        android:id="@+id/main_headerCounterBalance"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        />
</LinearLayout>

作为旁注,我在这里发现了一个问题,该问题解决了
android:layout\u weight
android:layout\u width
android:layout\u height
的使用问题,建议
weight
需要将相应的维度设置为
0dp
,否则,生成的大小将与
wrap\u content
match\u parent
给出的尺寸复合。果然,将所需的
weight
设置为
0dp
给了我一个更清晰的对齐方式,我注意到通过上述
.xml
显示的文本对齐方式更为直接


我还能够使用
android:gravity=“center”
而不是
android:textAlignment
,它提供了相同的居中对齐方式,而不会发出与所需API 15不兼容的警告。

我能够使用发布的备选答案实现所需对齐方式,关于幻影视图以平衡图像。然而,尽管作者坚持它应该在没有任何额外重量的情况下按原样工作,但我发现我仍然需要使用重量来达到所需的对齐

这是我的最终布局,经过调整以获得所需的结果,在我随问题发布的示例图像中

<LinearLayout
    android:id="@+id/main_headerLayout"
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    >

    <ImageView
        android:id="@+id/main_pipboyIcon"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:src="@drawable/pipboy"
        />

    <TextView
        android:id="@+id/main_headerText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:text="@string/main_title"
        android:gravity="center"
        android:textSize="20dp"
        android:textStyle="bold"
        />

    <View
        android:id="@+id/main_headerCounterBalance"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1"
        />
</LinearLayout>

作为旁注,我在这里发现了一个问题,该问题解决了
android:layout\u weight
android:layout\u width
android:layout\u height
的使用问题,建议
weight
需要将相应的维度设置为
0dp
,否则,生成的大小将与
wrap\u content
match\u parent
给出的尺寸复合。果然,将所需的
weight
设置为
0dp
给了我一个更清晰的对齐方式,我注意到通过上述
.xml
显示的文本对齐方式更为直接


我还可以使用
android:gravity=“center”
而不是
android:textAlignment
,它提供了相同的居中对齐方式,而不会发出与所需API 15不兼容的警告。

如果图像大小是静态的,可以使用TextView的drawabeLeft。尝试并为TextView提供drawablePadding。@RameshKumar,我已经根据您的评论探索了
drawableLeft
,但intellisense告诉我它在上下文中不存在,并且在尝试实现它时会立即出错。我可以在按钮上找到它的参考。也许这是因为我的图像没有静态大小,而是为了尝试