【问题标题】:Creating layout for ListView with a side index list使用侧索引列表为 ListView 创建布局
【发布时间】:2015-04-23 04:27:46
【问题描述】:

我为我的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%的屏幕)?

提前感谢您的帮助

【问题讨论】:

    标签: android xml list listview layout


    【解决方案1】:

    使用LinearLayout 而不是RelativeLayout

    保持根LinearLayout的方向horizo​​ntal,然后在其中创建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>
    

    【讨论】:

      【解决方案2】:

      您应该使用 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
      

      【讨论】:

        猜你喜欢
        • 2016-11-16
        • 2011-04-04
        • 1970-01-01
        • 2015-08-07
        • 2017-08-15
        • 1970-01-01
        • 1970-01-01
        • 2019-11-14
        • 2014-10-26
        相关资源
        最近更新 更多