【问题标题】:Android - ListItem RelativeLayout text in center of TextViews with height wrap_contentAndroid - 具有高度 wrap_content 的 TextViews 中心的 ListItem RelativeLayout 文本
【发布时间】:2014-07-08 20:04:44
【问题描述】:

我有一个 ListActivity 和一个使用 list_item.xml 的自定义 ArrayAdapter。在这个 xml 文件中,我有一个 ImageView 和三个 TextViews

ImageView 和前两个TextViews 靠左对齐,第三个TextView 靠右对齐。这一切都按预期工作。

现在我还想将TextViews 对齐在ListItem 的中心。

现在我使用RelativeLayout,因此我可以将第三个TextView 向右对齐,并且我在TextViews' layout_height 上使用match_parent,因为我在某处读过它需要对齐TextView 中的文本到 RelativeLayouts 中的中心。

这是我目前的list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- The View for a single CheckListItem -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:contentDescription="@string/checkbox_content_description"
        android:src="@drawable/checkbox_unchecked"
        android:background="@layout/transparent_background"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false" />

    <TextView
        android:id="@+id/tv_amount"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/image"
        android:layout_alignBottom="@id/image"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:layout_margin="5dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:textIsSelectable="false" />

    <!-- NOTE: layout_toLeftOf has @+id/ instead of @id to create the R.id here -->
    <TextView 
        android:id="@+id/tv_product_name"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/tv_amount"
        android:layout_toLeftOf="@+id/tv_price"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_alignBottom="@id/image"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:layout_margin="5dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:textIsSelectable="false"

        android:background="@layout/border" />
        <!-- TODO: Remove test border -->

    <!-- NOTE: id has @id instead of @+id, because we created the ID in the tv_product_name in the layout_toLeftOf -->
    <TextView
        android:id="@id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@id/image"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:layout_margin="5dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:textIsSelectable="false" />

</RelativeLayout>

结果如下:

我想要的是:


编辑 1:

在三个TextView中将android:gravity="center_vertical"改为android:gravity="center"后,Text只水平居中对齐,垂直居中不对齐:

【问题讨论】:

  • 改变重力=中心
  • @Sania 如果我在 TextViews 中将 gravity="center_vertical" 更改为 "center",则文本仅水平对齐到中心,而不是垂直对齐.. :S(参见编辑 1)
  • 抱歉改成水平居中
  • @KevinCruijssen 为 TextView 尝试 android:lineSpacingExtra
  • @Sania centercenter_horizontal 给出相同的结果(编辑 1 中的图片),而 center_vertical 由于某种原因不起作用..

标签: android android-layout textview android-relativelayout vertical-alignment


【解决方案1】:
//Try this way,i hope this will help you to solve your problem...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:contentDescription="@string/checkbox_content_description"
        android:src="@drawable/checkbox_unchecked"
        android:background="@layout/transparent_background"
        android:focusableInTouchMode="false"
        android:adjustViewBounds="true"
        android:clickable="false"
        android:focusable="false" />
    <TextView
        android:id="@+id/tv_amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:textIsSelectable="false" />


    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical">
    <TextView
        android:id="@+id/tv_product_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_margin="5dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:textIsSelectable="false"
        android:background="@layout/border" />
    </LinearLayout>

    <TextView
        android:id="@id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:textIsSelectable="false" />

</LinearLayout>

【讨论】:

  • 非常感谢,在将 LinearLayout's gravity 更改为 center_vertical 而不是 center 后,它完全符合我的要求。接受为答案。
【解决方案2】:

我只是编辑了你的一个 xml.try 用这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_layout"
   android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
  <TextView  android:id="@+id/floraCompStat"
             android:layout_width="wrap_content"
             android:layout_height="fill_parent"
             android:layout_alignBottom="@+id/image"
             android:background="@drawable/bg"
             android:gravity="center_vertical"
             android:text="TextView" />

bg.xml

 <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        >
        <solid android:color="#FFFFFF"/>
        <stroke android:width="1sp" android:color="#FF0000" />
        <padding android:left="15dp"/>
    </shape>

【讨论】:

    猜你喜欢
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2015-05-04
    相关资源
    最近更新 更多