【问题标题】:Vertically Center TextView in RelativeLayout在相对布局中垂直居中 TextView
【发布时间】:2010-07-09 19:48:24
【问题描述】:

在下图中,我希望红色的 textview 垂直居中,我想继续使用 RelativeLayout 而不是任何 LinearLayouts,我想我可以弄清楚如何使用 LinearLayout 但不想使用这些.

这一切都基于我在这里找到的示例android devleper's blog - Android Layout Tricks #1

这是我的 xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">

    <ImageView android:id="@+id/icon" android:layout_width="wrap_content"
        android:layout_height="fill_parent" 
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" 
        android:layout_alignParentRight="true"
        android:src="@drawable/chevron" />



    <TextView android:id="@+id/rightText" android:layout_width="55px"
        android:layout_height="fill_parent" android:layout_toLeftOf="@id/icon"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:textSize="14dip"
        android:text="$15.99" />


    <TextView android:id="@+id/secondLine" android:layout_width="wrap_content"
        android:layout_height="26dip" android:layout_toLeftOf="@id/rightText"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" 
        android:text="Simple application that shows how to use RelativeLayout" />

    <TextView android:layout_width="fill_parent" android:id="@+id/firstLine"
        android:layout_height="wrap_content" android:layout_toLeftOf="@id/rightText"
        android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
        android:layout_above="@id/secondLine"
        android:layout_alignWithParentIfMissing="true" android:gravity="center_vertical"
        android:singleLine="true"
        android:ellipsize="end"
        android:maxLines="1"
        android:textSize="13dip"
        android:text="My Application that has a really long title  asdfsdf asdf sdf asdf" />

</RelativeLayout>

【问题讨论】:

    标签: android textview android-relativelayout


    【解决方案1】:

    你只需要设置TextView的重力。

    【讨论】:

      【解决方案2】:

      如果以上答案不起作用,对其他人可能会有所帮助。只需在 textView 中设置以下属性

       android:layout_centerVertical="true"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-18
        • 2021-12-10
        • 2013-05-04
        • 2015-02-04
        • 2019-06-02
        • 2012-05-30
        • 1970-01-01
        相关资源
        最近更新 更多