【问题标题】:center text and image in RelativeLayout在 RelativeLayout 中居中文本和图像
【发布时间】:2018-03-08 00:01:50
【问题描述】:

我带着一个看似简单的问题来找你。但是我找不到合适的解决方案。 我在 LinearLayout 中的 RelativeLayout 中有一个 Image- 和一个 TextView。

<LinearLayout
    android:id="@+id/page2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:orientation="horizontal"
    android:weightSum="2"
    android:baselineAligned="false"
    android:divider="@drawable/separator_vertical"
    android:showDividers="middle">

    <RelativeLayout
        android:id="@+id/delete_button"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">


        <TextView
            android:id="@+id/delete_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="DemoText"
            android:textColor="#fff"
            android:textSize="15sp" />

        <ImageView

            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="10dp"
            android:layout_toStartOf="@id/delete_text"
            android:src="@drawable/ic_delete" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/edit_button"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <TextView
            android:id="@+id/edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="DemoText"
            android:textColor="#fff"
            android:textSize="15sp" />

        <ImageView

            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="10dp"
            android:layout_toStartOf="@id/edit_text"
            android:src="@drawable/ic_edit" />

    </RelativeLayout>


</LinearLayout>

目前只有TextView在各自的RelativeLayout中居中。 如何在 RelativeLayout 的中心对齐 ImageView 和 TextView?

【问题讨论】:

  • 尝试使用权重和宽度为 0 dp 的相对布局,使用 android:gravity="center" 希望它能解决您的问题.. :)
  • 谢谢解决了! :)
  • 好的,那我可以结束这个问题了。

标签: android android-layout android-relativelayout


【解决方案1】:

设置

android:gravity="center"

具有权重的父 relativeLayouts

这样它就可以按照你的意愿集中它的孩子了!!

【讨论】:

    【解决方案2】:

    您可以将两个视图包装在另一个布局中并将其居中。例如:

    <RelativeLayout
        android:id="@+id/delete_button"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center">
    
            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginEnd="10dp"
                android:src="@drawable/ic_delete" />
    
            <TextView
                android:id="@+id/delete_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="DemoText"
                android:textColor="#fff"
                android:textSize="15sp" />
    
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      LinearLayout 有不同的特点,你需要的是ConstraintLayout 和LinearLayout 里面。当然你可以用其他东西包裹RelativeLayout,但它是不必要的嵌套:

      <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/page2"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/colorPrimary"
      android:baselineAligned="false"
      android:orientation="horizontal"
      android:showDividers="middle"
      android:weightSum="2">
      
      <LinearLayout
          android:orientation="horizontal"
          android:id="@+id/delete_button"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          app:layout_constraintHorizontal_bias="0.5"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintEnd_toStartOf="@id/guideline">
      
      
          <ImageView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginEnd="10dp"
              android:layout_toStartOf="@id/delete_text"
              android:src="@drawable/delete_text" />
      
          <TextView
              android:id="@+id/delete_text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="DemoText"
              android:textColor="#fff"
              android:textSize="15sp" />
      </LinearLayout>
      
      <LinearLayout
          android:orientation="horizontal"
          android:id="@+id/edit_button"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          app:layout_constraintHorizontal_bias="0.5"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toEndOf="@id/guideline">
      
          <ImageView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginEnd="10dp"
              android:layout_toStartOf="@id/edit_text"
              android:src="@drawable/edit_text" />
      
          <TextView
              android:id="@+id/edit_text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="DemoText"
              android:textColor="#fff"
              android:textSize="15sp" />
      
      </LinearLayout>
      
      <android.support.constraint.Guideline
          android:id="@+id/guideline"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentEnd="true"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true"
          android:orientation="vertical"
          app:layout_constraintGuide_percent="0.5" />
      
      </android.support.constraint.ConstraintLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-22
        相关资源
        最近更新 更多