Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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_Android Relativelayout - Fatal编程技术网

Android 相对布局,对齐父视图中心和视图右侧,无重叠

Android 相对布局,对齐父视图中心和视图右侧,无重叠,android,android-relativelayout,Android,Android Relativelayout,我正在尝试将TextView对齐,使其在相对布局中居中,但也在ImageView的右侧。我希望它看起来像这样 [-(image)-----some text here------------] 我可以用下面的代码来实现这一点,但是如果文本太长,就会与图像重叠 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro

我正在尝试将TextView对齐,使其在相对布局中居中,但也在ImageView的右侧。我希望它看起来像这样

[-(image)-----some text here------------]
我可以用下面的代码来实现这一点,但是如果文本太长,就会与图像重叠

<?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"
    android:orientation="horizontal"
    android:paddingTop="@dimen/small_padding"
    android:paddingBottom="@dimen/small_padding"
    android:background="@color/White">

    <ImageView
        android:id="@+id/navMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/home_icon"
        android:layout_marginEnd="@dimen/small_padding"
        android:contentDescription="@string/abc_search_hint"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/actionBarTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/profile"
        android:textSize="24sp"
        android:textStyle="bold"
        android:textColor="@color/Black"
        android:maxLines="1"
        android:ellipsize="end"
        android:contentDescription="@string/title_activity_connect"
        android:layout_centerInParent="true" />

</RelativeLayout>


我曾尝试将TextView与ImageView的右侧对齐,但如果这样做,它将停止在父视图中居中。非常感谢您的任何帮助

您可以试试这样的方法。我使用了一个无线电组,而不是线性布局,但它仍然可以工作。像你已经做的那样,让线性布局水平,然后使布局重心,然后只需先放置图像,然后放置文本视图

<RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:paddingTop="20dp">

                    <RadioButton
                        android:id="@+id/radio_student"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/checkbox_student"
                        android:onClick="onRadioButtonClicked"
                        android:layout_marginEnd="30dp"
                        android:layout_marginRight="30dp"/>

                    <RadioButton
                        android:id="@+id/radio_teacher"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/checkbox_teacher"
                        android:onClick="onRadioButtonClicked"
                        android:layout_marginStart="30dp"
                        android:layout_marginLeft="30dp"/>

            </RadioGroup>

编辑: 我不知道我使用的按钮的边距属性是否适用于文本视图,但文本上剩余的填充可能有效

请尝试以下方法:

<?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"
    android:orientation="horizontal"
    android:paddingTop="@dimen/small_padding"
    android:paddingBottom="@dimen/small_padding"
    android:background="@color/White">

    <ImageView
        android:id="@+id/navMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/home_icon"
        android:layout_marginEnd="@dimen/small_padding"
        android:contentDescription="@string/abc_search_hint"
        android:layout_alignParentStart="`enter code here`true" />

    <TextView
        android:id="@+id/actionBarTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/profile"
        android:textSize="24sp"
        android:textStyle="bold"
        android:textColor="@color/Black"
        android:maxLines="1"
        android:ellipsize="end"
        android:contentDescription="@string/title_activity_connect"
        android:layout_toRightOf="@+id/navMenu"
        android:alignParentRight="true"
        android:gravity="center" />

</RelativeLayout>

您可以(并且应该,为了性能起见)在文本视图中使用一个复合可绘制的

您希望图像保持在文本视图的左侧,因此请使用
android:drawablelft

<?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"
    android:paddingTop="@dimen/small_padding"
    android:paddingBottom="@dimen/small_padding"
    android:background="@color/White"
    >
    <TextView
        android:id="@+id/actionBarTitle"
        android:drawableLeft="@drawable/home_icon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/profile"
        android:textSize="24sp"
        android:textStyle="bold"
        android:textColor="@color/Black"
        android:maxLines="1"
        android:ellipsize="end"
        android:contentDescription="@string/title_activity_connect"
        android:centerInParent="true"
        android:gravity="center"
    />    
</RelativeLayout>


不能将重力设置为相对布局内的元素。如果设置了重力,则会将文本居中放置在TextView内。我需要将TextView居中于父相对布局内。忘记了一些事情,请将TextView LayoutWidt=matchparent@fahadalawam你误解了这个人想要什么。这仍将对齐
TextView
中心的文本,而不是
RelativeLayout
我的问题是重叠,而不是对齐。此外,我不使用线性布局,而是使用相对布局。这样,我就可以将textview居中对齐,同时将imageview放置在左侧。由于有如此多的显示器和如此多的尺寸,使用边距来尝试解决这些问题是行不通的。这里的问题是,这也会将图像移动到布局的中心。如果我更改width属性以匹配parent,它几乎可以工作。但是,文本将居中于imageview右侧的剩余空间中。我需要图像保持左对齐,文本保持在整个父布局的中心。这就是为什么这很棘手的原因ImageView是否需要位于TextView之上并允许文本在其后面填充?或者它是否需要像图像一样始终位于文本的左侧,而文本视图的左侧由其限定?@DanielGruici这是一个水平布局。图像视图必须保持完全向左对齐,文本必须在父布局中居中,而不是图像右侧的剩余空间。当前代码的工作方式是,直到文本变得太长,然后它与左边的imageview重叠。我唯一能想到的是带权重的线性布局。创建水平布局,左侧为ImageView,中间为TextView,右侧为空白视图。为图像和空白视图赋予相等的权重,为文本视图赋予较大的权重。然后可以设置文字重力,将始终居中,并由其中的其他两个视图绑定。“你可能得摆弄一下重物,才能让它看起来像你想要的样子。”DanielGruici ya,我想无论哪种方式,都必须是一种很有技巧的工作。如果在相对布局中强制一个视图位于另一个视图的左侧或右侧,则该视图将自动覆盖父视图。不幸的是,我找到的唯一解决方案是为文本提供maxLength并将其省略。因此,它不会与其他视图重叠。