繁体   English   中英

如何对齐imageview和屏幕android中心的两个文本?

[英]How to align imageview, and two text at center of screen android?

嗨,我的父布局为LinearLayout,我需要在中心放置一个imageview,然后在屏幕中心一个接一个地放置两个textview。 屏幕底部是一个图标。

我放置了所有内容,但是imageview和两个textview没有在屏幕中心对齐。 这是布局。

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/bg"
    android:orientation="vertical">

   //this image and two textview, i need to place at center of screen
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/seat_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/on_logo"
        android:layout_marginBottom="@dimen/dp_size20"
        />

    <TextView
        android:id="@+id/seat_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test1"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="16dp"
        android:gravity="center"/>
    <TextView
        android:id="@+id/another_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/dp_size10"
        android:paddingBottom="@dimen/dp_size10"
        android:text="TEst 2"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="16dp"
        android:gravity="center"/>

</LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/dp_size10">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true">

            <ImageView
                android:id="@+id/on_boarding_circle_arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_centerInParent="true"
                android:background="@drawable/circle_arrow" />
</RelativeLayout>
    </RelativeLayout>
    </LinearLayout>

我真的无法弄清楚我在这个设计中做了什么错误。 ImageView和两个文本位于屏幕顶部而不是屏幕中心。

我已经改变了一些尺寸和图像,纠正了这些,你很高兴。

尝试这个..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_centerInParent="true"
            android:orientation="vertical">
            <ImageView
                android:id="@+id/seat_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@mipmap/ic_launcher"
                android:layout_marginBottom="20dp" />
            <TextView
                android:id="@+id/seat_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Test1"
                android:textAllCaps="false"
                android:textSize="16dp"
                android:gravity="center"/>
            <TextView
                android:id="@+id/another_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="@dimen/dp_size10"
                android:paddingBottom="@dimen/dp_size10"
                android:text="TEst 2"
                android:textAllCaps="false"
                android:textColor="@color/white"
                android:textSize="16dp"
                android:gravity="center"/>

        </LinearLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="@dimen/dp_size10">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="1"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true">

                <ImageView
                    android:id="@+id/on_boarding_circle_arrow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_centerInParent="true"
                    android:background="@mipmap/ic_launcher" />
            </RelativeLayout>
        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>
<?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="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <LinearLayout
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/seat_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            />

        <TextView
            android:id="@+id/seat_number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Test1"
            android:textAllCaps="false"
            android:textColor="@color/Grey"
            android:textSize="16dp"
            android:gravity="center"/>
        <TextView
            android:id="@+id/another_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:text="TEst 2"
            android:textAllCaps="false"

            android:textSize="16dp"
            android:gravity="center"/>

    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            >

            <ImageView
                android:id="@+id/on_boarding_circle_arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_centerInParent="true"
                android:background="@drawable/acircle" />
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

问题是你正在使用线性布局,所以它是一个接一个地对齐布局。所以你需要使用相对布局,它会工作

试试这个代码

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/bg"
    android:orientation="vertical">

   //this image and two textview, i need to place at center of screen
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/seat_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/on_logo"
        android:layout_marginBottom="@dimen/dp_size20"
        />

    <TextView
        android:id="@+id/seat_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test1"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="16dp"
        android:gravity="center"/>
    <TextView
        android:id="@+id/another_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/dp_size10"
        android:paddingBottom="@dimen/dp_size10"
        android:text="TEst 2"
        android:textAllCaps="false"
        android:textColor="@color/white"
        android:textSize="16dp"
        android:gravity="center"/>

</LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/dp_size10">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true">

            <ImageView
                android:id="@+id/on_boarding_circle_arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_centerInParent="true"
                android:background="@drawable/circle_arrow" />
</RelativeLayout>
    </RelativeLayout>
    </LinearLayout>

底部的RelativeLayout是问题所在。 并将android:gravity="center"到您的父LinearLayout

只需在你的根LinearLayout中添加android:gravity =“center”并删除最后的RelativeLayout并查看

从子LinearLayout中剪切'layout_gravity'和'gravity'参数并将其粘贴到Parent LinearLayout

<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical">

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:orientation="vertical">
            <ImageView
                 android:id="@+id/seat_icon"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
         />
 Your code....

 >

暂无
暂无

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

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