使用边索引列表为 ListView 创建布局

Creating layout for ListView with a side index list

我为 ListView 创建了这个布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView 
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:dividerHeight="10.0sp"
        android:background="#4a4c4d">
    </ListView>

    <LinearLayout android:orientation="vertical"
        android:layout_width="28dip"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@android:color/transparent" >

        <TextView android:id="@+id/A"
            android:text="A"
            android:tag="A"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
        <TextView android:id="@+id/B"
            android:text="B"
            android:tag="B"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/C"
            android:text="C"
            android:tag="C"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/D"
            android:text="D"
            android:tag="D"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/E"
            android:text="E"
            android:tag="E"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/F"
            android:text="F"
            android:tag="F"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/G"
            android:text="G"
            android:tag="G"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/H"
            android:text="H"
            android:tag="H"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/I"
            android:text="I"
            android:tag="I"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/J"
            android:text="J"
            android:tag="J"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/K"
            android:text="K"
            android:tag="K"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/L"
            android:text="L"
            android:tag="L"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/M"
            android:text="M"
            android:tag="M"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/N"
            android:text="N"
            android:tag="N"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/O"
            android:text="O"
            android:tag="O"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/P"
            android:text="P"
            android:tag="P"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/Q"
            android:text="Q"
            android:tag="Q"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/R"
            android:text="R"
            android:tag="R"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/S"
            android:text="S"
            android:tag="S"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/T"
            android:text="T"
            android:tag="T"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/U"
            android:text="U"
            android:tag="U"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/V"
            android:text="V"
            android:tag="V"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/W"
            android:text="W"
            android:tag="W"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/X"
            android:text="X"
            android:tag="X"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/Y"
            android:text="Y"
            android:tag="Y"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView android:id="@+id/Z"
            android:text="Z"
            android:tag="Z"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</RelativeLayout>

但是垂直 LinearLayout 似乎重叠在 ListView 上,而不是在 ListView 的右侧。如何修复上面的代码,让 ListView 在左边(覆盖大约 90% 的屏幕)和垂直 LinearLayout 在右边(覆盖屏幕的剩余 10%)?

提前感谢您的帮助

使用 LinearLayout 而不是 RelativeLayout

保持根 LinearLayout 的方向 水平,然后在其中创建 2 个 linearLaytout,一个 weight="0.9" 另一个 weight="0.1"

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.9">

        <ListView 
            android:id="@+id/android:list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:dividerHeight="10.0sp"
            android:background="#4a4c4d">
        </ListView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.1">
        <TextView .../>
        <TextView .../>
        <TextView .../>
        .
        . 
        .
    </LinearLayout>
</LinearLayout>

您应该使用 LinearLayout 作为父布局而不是 RelativeLayout。还为 android 屏幕的覆盖部分分配布局权重 属性。希望这段代码对你有用。

enter code here

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

    <ListView
        android:id="@+id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#4a4c4d"
        android:layout_weight=".9"
        android:dividerHeight="10.0sp" >
    </ListView>

    <LinearLayout
        android:layout_width="28dip"
        android:layout_height="wrap_content"
        android:layout_weight=".1"
        android:background="@android:color/transparent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/A"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="A"
            android:text="A" />

        <TextView
            android:id="@+id/B"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="B"
            android:text="B" />

        <TextView
            android:id="@+id/C"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="C"
            android:text="C" />

        <TextView
            android:id="@+id/D"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="D"
            android:text="D" />

        <TextView
            android:id="@+id/E"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="E"
            android:text="E" />

        <TextView
            android:id="@+id/F"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="F"
            android:text="F" />

        <TextView
            android:id="@+id/G"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="G"
            android:text="G" />

        <TextView
            android:id="@+id/H"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="H"
            android:text="H" />

        <TextView
            android:id="@+id/I"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="I"
            android:text="I" />

        <TextView
            android:id="@+id/J"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="J"
            android:text="J" />

        <TextView
            android:id="@+id/K"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="K"
            android:text="K" />

        <TextView
            android:id="@+id/L"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="L"
            android:text="L" />

        <TextView
            android:id="@+id/M"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="M"
            android:text="M" />

        <TextView
            android:id="@+id/N"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="N"
            android:text="N" />

        <TextView
            android:id="@+id/O"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="O"
            android:text="O" />

        <TextView
            android:id="@+id/P"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="P"
            android:text="P" />

        <TextView
            android:id="@+id/Q"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="Q"
            android:text="Q" />

        <TextView
            android:id="@+id/R"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="R"
            android:text="R" />

        <TextView
            android:id="@+id/S"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="S"
            android:text="S" />

        <TextView
            android:id="@+id/T"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="T"
            android:text="T" />

        <TextView
            android:id="@+id/U"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="U"
            android:text="U" />

        <TextView
            android:id="@+id/V"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="V"
            android:text="V" />

        <TextView
            android:id="@+id/W"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="W"
            android:text="W" />

        <TextView
            android:id="@+id/X"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="X"
            android:text="X" />

        <TextView
            android:id="@+id/Y"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="Y"
            android:text="Y" />

        <TextView
            android:id="@+id/Z"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:tag="Z"
            android:text="Z" />
    </LinearLayout>
</LinearLayout>
enter code here