繁体   English   中英

如果图像位于布局的左侧,如何将文本放置在布局的中心?

[英]How to place the text to center of the layout if image is in left side of the layout?

在相对布局中,我在布局的左侧使用了一个图像,并且textview应该在图像的右侧,并且我需要textview应该在Reltaive布局的中心。 谁能帮助我实现这一目标?

码:

 <RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.z003tcnc.myapplication.MainActivity">

<ImageView
    android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text1"
    android:text="Hello World"
    android:maxLines="1"
    android:ellipsize="end"
    android:layout_centerHorizontal="true"
    android:layout_toRightOf="@+id/img1"
  />
</RelativeLayout>

我需要这样的输出

如果要让textview处于相对布局的中心,请在TextView中添加以下行。

   android:layout_centerInParent="true"

我想这就是你要找的

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/ic_action_home" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="hello" />
</RelativeLayout>

这就是它的样子

在此处输入图片说明

如果需要,可以将其放入

根据您的需要设置保证金。

android:layout_marginLeft="100dp"

假定图像的高度大于文本视图的高度。

在TextView上使用alignTopalignBottom ,以使其覆盖与图像相同的高度。 然后使用android:gravity="center"将文本在TextView中android:gravity="center"

编辑::添加代码以在相对布局中同时显示两个视图

这是代码:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:gravity="center">

        <ImageView
            android:id="@+id/img1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text1"
            android:text="Hello World"
            android:maxLines="1"
            android:ellipsize="end"
            android:layout_alignTop="@+id/img1"
            android:layout_alignBottom="@+id/img1"
            android:gravity="center"
            android:layout_toRightOf="@+id/img1"
            />
    </RelativeLayout>

注意:我已经测试了xml布局和它的工作原理

仅更改两行:
将父级相对布局高度作为包装内容,并在TextView中添加CenterVertical为true,并根据需要从左侧开始设置一些边距。

这是代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<ImageView
    android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_home"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text1"
    android:text="Hello World"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>


[1]: https://i.stack.imgur.com/Mwkm8.png

在这里,而不是使用两个单独的组件,而是使用:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world"
        android:maxLines="1"
        android:ellipsize="end"
        android:textSize="22sp"
        android:textColor="#000"
        android:gravity="center"
        android:drawablePadding="16dp"
        android:paddingEnd="16dp"
        android:drawableLeft="@mipmap/ic_launcher"/>

根据需要更改参数。

希望能帮助到你!!!

从TextView中删除以下行:

机器人:layout_toRightOf = “@ + ID / IMG1”

<ImageView
    android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text1"
    android:text="Hello World"
    android:maxLines="1"
    android:ellipsize="end"
    android:layout_alignBottom="@+id/img1"
    android:layout_centerHorizontal="true" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM