【问题标题】:Aligning center relative to others相对于其他对齐中心
【发布时间】:2011-10-15 17:13:01
【问题描述】:

因此可以将一个视图的顶部、底部、左侧和右侧与另一个视图对齐,以使它们的边缘彼此齐平。但是,我一直想知道是否可以对齐不同大小的两个视图的中心。当我将 ImageView 与 TextView 并排放置时,就会出现我的情况,如下所示:[ImageView] [TextView]。 ImageView 比 TextView 高一点,所以我要做的是在 TextView 的底部添加填充/边距以使其对齐并看起来像 ImageView 和 TextView 水平中心对齐。问题是,当此视图显示在较大的平板电脑屏幕上时,填充和边距无法正常工作,并且 TextView 看起来与 ImageView 不一致。我确信有一个简单的解决方法可以让这个问题总是解决,所以有人可以为我提供一些见解吗?谢谢!

【问题讨论】:

    标签: android view padding center alignment


    【解决方案1】:

    这就是我对列表视图行所做的:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="#FFFFFF"
      android:padding="5dp"
      android:gravity="center_vertical">
      <TextView android:id="@+id/questionItemTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20sp"
        android:textColor="#000000"
        android:gravity="center_vertical"/>
      <ImageView android:id="@+id/questionViewed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/eye"
        android:layout_marginLeft="5dp" />
      <ImageView android:id="@+id/questionAnswered"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/check"
        android:layout_marginLeft="5dp" />
    </LinearLayout>
    

    【讨论】:

    • 只需要修改它,但我认为center_vertical 让它工作。谢谢!
    • 是的,对于 LinearLayout 和 TextView 的 android:gravity=center_vertical 应该可以解决问题。
    【解决方案2】:

    使用 weightSum 而不是 dips 或 pxs 中的填充,这将使您的布局看起来很好,无论用户设备的大小如何。

    或者你也可以使用重力。

    或者您可以创建第二个 xml 文件并将其插入到 res/xlarge 文件中,手机将选择要读取的 xml。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 2016-05-17
      • 2013-05-06
      • 2013-11-27
      • 1970-01-01
      相关资源
      最近更新 更多