【问题标题】:Is there a way to align TextView vertically center with ImageView in RelativeLayout有没有办法将 TextView 垂直居中与相对布局中的 ImageView 对齐
【发布时间】:2014-05-22 12:22:01
【问题描述】:

我有一个RelativeLayout,左边是ImageViewTextView,右边是ImageViewTextView

但它们与 ImageView 没有很好地对齐。我希望它垂直居中对齐文本视图旁边的图像

现在是这样的:

我使用的 RelativeLayout 是这样的:

问题

如何使TextViewImageView 很好地对齐

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/leftIcon"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/trash" />

    <TextView
        android:id="@+id/tv1"
        android:layout_toRightOf="@+id/leftIcon"
        android:layout_toLeftOf="@+id/iv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:text="some text 1"
        android:layout_marginLeft="5dp"
        android:textColor="#000000"
        android:textStyle="bold"/>

    <ImageView
        android:id="@+id/iv2"
        android:layout_toLeftOf="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/trash" />

    <TextView
        android:layout_alignParentRight="true"
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14dp"
        android:textColor="#000000"
        android:text="some text 2"
        android:textStyle="bold"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="25dp" />
</RelativeLayout>

【问题讨论】:

  • “对齐良好”是什么意思?你到底要达到什么目标?
  • 你试过android:layout_centerVertical="true"吗?
  • 我假设您希望图像与文本的基线对齐(而不是与 textView 的底部对齐)。 android:layout_alignBaseline 可能就是你要找的。​​span>
  • @FarhadFaghihi 我正在尝试将 TextView 垂直居中对齐到它旁边的图像
  • @birdy :您是否为 ImageViews 和 TextViews 都尝试过 android:layout_centerVertical="true"?他们都需要具有该属性。

标签: android android-layout


【解决方案1】:
hi try this code i think it will help you : 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="15dp"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal" >

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

        <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="some text 1"
            android:textColor="#ffffff"
            android:textSize="14dp"
            android:textStyle="bold" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

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

        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="25dp"
            android:text="some text 2"
            android:textColor="#ffffff"
            android:textSize="14dp"
            android:textStyle="bold" />

    </LinearLayout>

</RelativeLayout>

【讨论】:

    【解决方案2】:
    // Try this way,hope this will help you to solve your problem...
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:orientation="horizontal">
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center">
            <ImageView
                android:id="@+id/leftIcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher"
                android:adjustViewBounds="true"/>
    
            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:text="some text 1"
                android:layout_marginLeft="5dp"
                android:textColor="#000000"
                android:textStyle="bold"/>
    
            </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical|right">
            <ImageView
                android:id="@+id/iv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher"
                android:adjustViewBounds="true"/>
    
            <TextView
                android:id="@+id/tv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textColor="#000000"
                android:text="some text 2"
                android:textStyle="bold"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="25dp" />
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="15dp"
          android:layout_marginBottom="10dp"
          android:layout_marginLeft="10dp"
          android:orientation="horizontal"
      
          >
      
          <ImageView
              android:id="@+id/leftIcon"
              android:layout_alignParentLeft="true"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/ic_launcher" />
      
          <ImageView
              android:id="@+id/iv2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"
              android:layout_toLeftOf="@+id/tv2"
              android:src="@drawable/ic_launcher" />
      
          <TextView
              android:id="@+id/tv1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_below="@+id/tv2"
              android:layout_toRightOf="@+id/leftIcon"
              android:text="some text 1"
              android:textColor="#000000"
              android:textSize="14dp"
              android:textStyle="bold" />
      
          <TextView
              android:id="@+id/tv2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true"
              android:layout_centerVertical="true"
              android:layout_marginRight="26dp"
              android:text="some text 2"
              android:textColor="#000000"
              android:textSize="14dp"
              android:textStyle="bold" />
      
      </RelativeLayout>
      

      【讨论】:

      • 请问-1的原因
      • 是的 - 您发布的任何内容如何解决垂直对齐 TextViews 中心的问题?阅读问题标题。
      • 不,看起来不太好。首先,您似乎已经决定 OP 想要 ic_launcher 作为他们的可绘制对象,而不是 trash 可绘制对象。您已将 android:layout_alignParentTop="true" 属性添加到第二个 ImageView 但不是第一个,然后您已将 android:layout_centerVertical="true" 添加到第二个 TextView 但不是第一个。
      猜你喜欢
      • 1970-01-01
      • 2015-02-04
      • 2012-08-27
      • 2015-10-11
      • 1970-01-01
      • 2012-05-30
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多