【问题标题】:How to align imageview, and two text at center of screen android?如何对齐imageview和屏幕android中心的两个文本?
【发布时间】:2016-11-30 18:31:55
【问题描述】:

嗨,我的父布局为 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 和两个文本放置在屏幕顶部而不是屏幕中心。

【问题讨论】:

  • 你的输出怎么样?显示屏幕

标签: android android-layout android-xml


【解决方案1】:

我已经更改了一些尺寸和图像,纠正这些,你就可以开始了。

试试这个..

<?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>

【讨论】:

    【解决方案2】:
    <?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>
    

    问题是您使用的是线性布局,因此它会一个接一个地对齐布局。所以您需要使用相对布局,它会起作用

    【讨论】:

      【解决方案3】:

      试试这个代码

       <?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>
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          只需在根 LinearLayout 中添加 android:gravity="center" 并删除最后一个 RelativeLayout 并查看

          【讨论】:

            【解决方案6】:

            从子 LinearLayout 中剪切 'layout_gravity' 和 'gravity' 参数并将其粘贴到父 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....
            

             >
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-06-16
              • 1970-01-01
              • 1970-01-01
              • 2017-08-19
              • 2020-05-14
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多