Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-ListItem RelativeLayout文本位于文本视图中心,包含高度换行内容_Android_Android Layout_Textview_Android Relativelayout_Vertical Alignment - Fatal编程技术网

Android-ListItem RelativeLayout文本位于文本视图中心,包含高度换行内容

Android-ListItem RelativeLayout文本位于文本视图中心,包含高度换行内容,android,android-layout,textview,android-relativelayout,vertical-alignment,Android,Android Layout,Textview,Android Relativelayout,Vertical Alignment,我有一个ListActivity,带有一个自定义ArrayAdapter,它使用list\u item.xml。在这个xml文件中,我有一个ImageView和三个textview ImageView和前两个TextView在左侧相邻对齐,第三个TextView在右侧对齐。这一切都是有意的 现在,我还想将列表项中心的文本视图全部对齐 现在我使用了一个相对视图,这样我就可以将第三个文本视图对齐到右边,并且我在文本视图的布局高度上使用了匹配父视图,因为我在某个地方读到需要将文本视图中的文本对齐到相对

我有一个
ListActivity
,带有一个自定义
ArrayAdapter
,它使用
list\u item.xml
。在这个xml文件中,我有一个
ImageView
和三个
textview

ImageView
和前两个
TextView
在左侧相邻对齐,第三个
TextView
在右侧对齐。这一切都是有意的

现在,我还想将
列表项
中心的
文本视图
全部对齐

现在我使用了一个
相对视图
,这样我就可以将第三个
文本视图
对齐到右边,并且我在
文本视图的布局高度
上使用了
匹配父视图
,因为我在某个地方读到需要将
文本视图
中的文本对齐到
相对视图
的中心

这是我当前的
列表\u 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:

在三个文本视图中将
android:gravity=“center\u vertical”
更改为
android:gravity=“center”
后,文本仅在水平方向上居中对齐,而不是垂直对齐:


i jsut编辑了您的一个xml。试试这个

<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>

//试试这个方法,我希望这能帮助你解决你的问题。。。

改变重力=center@Sania如果我在文本视图中将
gravity=“center\u vertical”
更改为
“center”
,则文本仅在水平方向上与中心对齐,而不是在垂直方向上对齐S(请参见编辑1)抱歉,请将其更改为居中horizontal@KevinCruijssen请尝试android:lineSpacingExtraTextView@Sania
center
center\u horizontal
都给出了相同的结果(编辑1中的图片),而
center\u horizontal
由于某些原因不起作用..非常感谢,将
LinearLayout的重力
更改为
center\u vertical
而不是
center
后,它的工作方式完全符合我的要求。被接受为答案。
//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>