以编程方式设置expandablelistview项的高度

时间:2012-05-16 13:46:59

标签: android layout height expandablelistview

我有一个expandablelistview,我已经为groups项目添加了背景,但我想调整高度。我已经将高度设置为xml文件(50dp),但它不起作用(实际高度不是50 dp,但它更高),所以我想以编程方式进行。

enter image description here

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

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> 

Index.java

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

IndexAdapter.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));
*/              
   }

有人可以帮助我吗?

5 个答案:

答案 0 :(得分: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”让列表视图中的行自行调整大小。

答案 1 :(得分:0)

使用此代码设置父高和子高度

  

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

答案 2 :(得分:0)

我已经成功实现了屏幕右侧的群组指示,没有任何问题,可以通过向群组布局添加额外的imageview,如下所示: -

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

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

答案 3 :(得分: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));

答案 4 :(得分:0)

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