【问题标题】:Center vertically textview inside a list item在列表项内垂直居中文本视图
【发布时间】:2012-07-18 12:16:43
【问题描述】:

我正在开发一个具有列表视图的 Android 2.3.3 应用程序。

这是列表 layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gatesLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/btnAddGate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/add_r"
            android:contentDescription="@string/layoutEmpty"
            android:onClick="onAddGateClick" />

        <ImageButton
            android:id="@+id/btnDeleteGate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/delete_n"
            android:contentDescription="@string/layoutEmpty"
            android:onClick="onDeleteGateClick" />

    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_marginTop="40dip"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

这是 list_item.xml:

<?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="40dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/gateTypeImage"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_gravity="left|center_horizontal"
        android:layout_weight=".2"
        android:contentDescription="@string/layoutEmpty" />

    <TextView
        android:id="@+id/gateNameText"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical|center"
        android:layout_weight=".7"
        android:textSize="15dp" />

    <CheckBox
        android:id="@+id/gateSelectedCheck"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_gravity="right|center_vertical"
        android:layout_weight=".1" />

</LinearLayout>

我不知道如何使gateNameText 文本垂直居中。我把它放在左上角的顶部。

我该怎么做?

【问题讨论】:

    标签: android layout


    【解决方案1】:

    像这样使用android:gravity

    TextView
            android:id="@+id/gateNameText"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:gravity="center_vertical|center"
            android:layout_weight=".7"
            android:textSize="15dp" />
    

    【讨论】:

      【解决方案2】:

      你想使用:

      android:gravity="center_vertical|center"
      

      【讨论】:

        【解决方案3】:

        试试这个 list_item.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="40dp" >
        
            <ImageView
                android:id="@+id/gateTypeImage"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_weight=".2"
                android:background="@drawable/ic_launcher"
                android:contentDescription="layoutEmpty" />
        
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true" >
        
                <TextView
                    android:id="@+id/gateNameText"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@+id/gateTypeImage"
                    android:layout_weight=".7"
                    android:gravity="center_vertical"
                    android:text="ABCDEFGH"
                    android:textSize="15dp" />
            </RelativeLayout>
        
            <CheckBox
                android:id="@+id/gateSelectedCheck"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_weight=".1" />
        
        </RelativeLayout>
        

        【讨论】:

          猜你喜欢
          • 2012-03-12
          • 1970-01-01
          • 2016-02-05
          • 2015-02-28
          • 1970-01-01
          • 2020-05-10
          • 2011-11-28
          • 1970-01-01
          • 2013-10-15
          相关资源
          最近更新 更多