0

我有一个可扩展列表视图,并且为组项设置了背景,但我想调整高度。我已经将高度设置为 xml 文件(50dp),但它不起作用(实际高度不是 50 dp,但它更高),所以我正在考虑以编程方式进行。

在此处输入图像描述

这是我的一段代码来解释它

grouplayout_index.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/LinearLayout_index"
      android:background="@drawable/bg_capitolo_retina"
      android:paddingBottom="10dp"
      android:paddingTop="10dp"
      android:layout_height="50dp"             ---------------------//height = 50 dp
      android:layout_marginTop="10dp"
      android:layout_marginBottom="10dp"
      android:layout_width="match_parent" >

    <ImageView android:id="@+id/freccia_indice" android:src="@drawable/freccia_o"
        android:clickable="false"
        android:layout_height="28dp"
        android:layout_width="28dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp" 
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:paddingLeft="10dp"  />

    <TextView android:id="@+id/tvGroup"  
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="10dp"
        android:paddingLeft="50dp" />
</LinearLayout> 

索引.java

  public class Index extends  ExpandableListActivity {
        .....
           IndexAdapter   mAdapter = new IndexAdapter .....
          explistview.setAdapter(mAdapter);
}

索引适配器.java

   public class IndexAdapter  extends BaseExpandableListAdapter{

     ....various methods......
   public View getGroupView(int groupPosition, boolean isExpanded, View  convertView,ViewGroup parent) {

    if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             convertView =   infalInflater.inflate(R.layout.grouplayout_index, null);

    }      
         TextView textView = (TextView) convertView.findViewById(R.id.tvGroup);
         textView.setTextAppearance(context, R.style.textViewStyleGroup);
         textView.setText("textview");

            /* I have tried this code but it doesn't works.
           LinearLayout ll = (LinearLayout)   convertView.findViewById(R.id.LinearLayout_indice);
           ll.setLayoutParams(new LinearLayout.LayoutParams(30,50));
*/              
   }

有人能帮助我吗?

4

5 回答 5

1

额外高度的问题可能是在 LinearLayout 上设置的 layout_marginTop 和 layout_marginBottom 的结果。如下布局可能满足您的需求:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout_index"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/shape_data_background"
android:paddingBottom="10dp"
android:paddingTop="10dp" >

<ImageView
    android:id="@+id/freccia_indice"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:paddingLeft="10dp"
    android:src="@drawable/arrow_right" />

<TextView
    android:id="@+id/tvGroup"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="textview" />

</LinearLayout>

除非您对正好为 50dp 的行有要求,否则我通常最好使用 layout_height="wrap_content" 让列表视图中的行自行调整大小。

于 2012-05-16T14:27:03.573 回答
0

使用此代码设置父子高度

expListView.setChildDivider(getResources().getDrawable(R.color.white)); expListView.setDivider(getResources().getDrawable(R.color.white));
expListView.setDividerHeight(20);

于 2014-07-15T09:32:19.123 回答
0

通过向组布局添加额外的图像视图,我已经成功地在屏幕右侧实现了组指示器,没有任何问题,如下所示: -

Expandable Adapter method 

@Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        View row;

        if (convertView != null)
        {
            row = convertView;
        }
        else
        {
            row = LayoutInflater.from(getContext()).inflate(R.layout.group_row, null));
        }


        final ImageView indicator = (ImageView)row.findViewById(R.id.indicator);
        indicator.setSelected(isExpanded);
        return row;
    }

group layout  

<?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="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp" 
    android:background="#fff">


    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingLeft="3dp"
        android:paddingRight="20dp"
        android:textSize="16dp"
        android:text="Header"
        android:lineSpacingExtra="-4dp"
        android:textColor="@color/title_heading" />

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        >
    </LinearLayout>

     <ImageView 
         android:id="@+id/imageview_list_group_indicator"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginRight="7dp"
        android:src="@drawable/groupindicator1"/>
</LinearLayout>

图像的选择器 xml

<?xml version="1.0" encoding="utf-8"?>


 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
       <item android:drawable="@drawable/arrow_up" android:state_selected="true" />
    <item android:drawable="@drawable/arrow_down" android:state_selected="false" />
    <item android:drawable="@drawable/arrow_down"  />
</selector>
于 2014-07-16T06:57:39.040 回答
0

像这样使用

ExpandableAdapter adapter = new 


ExpandableAdapter(Fragment_Test.this.getActivity(), parentList, childList);
        ExpandableListView exp = new ExpandableListView(Fragment_Test.this.getActivity());
        exp.setGroupIndicator(null);
        exp.setSelector(getResources().getDrawable(R.drawable.selectedcorner));
        exp.setVerticalScrollBarEnabled(true);
        exp.setScrollbarFadingEnabled(false);
    exp.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
于 2015-03-12T06:39:12.663 回答
0

如果您正在使用数据绑定并且在展开时如果可扩展列表视图没有调整高度,那么我建议调用 binding.executependingBinding()

于 2021-02-09T13:46:24.687 回答