ExpandableListView子宽度

时间:2018-03-05 21:47:43

标签: java android expandablelistview

enter image description here我的应用中有一个ExpandableListView。我想让子布局比组头更窄,并保持居中。由于一些视觉效果,我必须从代​​码设置布局参数,但我似乎找不到这个尝试改变布局宽度从xml没有运气的最佳解决方案。任何建议都会有所帮助。 感谢

adapter getChild()方法:

 @Override
  public View getChildView(final int groupPosition, final int 
childPosition,
                         boolean isLastChild, View convertView, 
ViewGroup parent) {

    final Item expandedListitem = (Item) getChild(groupPosition, childPosition);

    Drawable drawable= ContextCompat.getDrawable(context,R.drawable.background_border);
    GradientDrawable gradientDrawable= (GradientDrawable) drawable;
    gradientDrawable.setStroke(5,expandedListitem.getColor());

    if (Build.VERSION.SDK_INT >= 19) {


        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item, null);

        }


        if (!isLastChild) {
            View divider = convertView.findViewById(R.id.linearfaq);
            divider.setVisibility(View.INVISIBLE);
            ViewGroup.LayoutParams params = convertView.getLayoutParams();
            int width = (MainActivity.display.getWidth());
            params.height = 110;
            params.width=width-40;
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            params.addRule(RelativeLayout.CENTER_HORIZONTAL);
             convertView.setLayoutParams(params);
            View padder = convertView.findViewById(R.id.padder);
            padder.setVisibility(View.INVISIBLE);

        } else {
            View divider = convertView.findViewById(R.id.linearfaq);
            RelativeLayout.LayoutParams params = new 


[![enter image description here][1]][1]RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT);
            int width = (MainActivity.display.getWidth());
            params.height = 140;
            params.width=width-40;
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            params.addRule(RelativeLayout.CENTER_HORIZONTAL);               
            convertView.setLayoutParams(params);
            divider.setVisibility(View.VISIBLE);
            View padder = convertView.findViewById(R.id.padder);
            padder.setBackgroundColor(expandedListitem.getColor());
            padder.setVisibility(View.VISIBLE);

        }
    }else{
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item_low, null);

        }
    }

项目布局:

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

 <RelativeLayout android:layout_width="match_parent"
   android:layout_height="wrap_content"
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:background="@color/back" >

  <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"

  android:layout_centerHorizontal="true"
android:layout_centerInParent="true"


>

<CheckBox
    android:layout_alignParentLeft="true"
    android:layout_marginTop="2dp"
    android:layout_marginEnd="8dp"

    android:clickable="true"
    android:focusableInTouchMode="false"
    android:layout_marginRight="8dp"
    android:layout_alignParentStart="true"
    android:layout_width="wrap_content"
     android:focusable="false"
    android:layout_height="wrap_content"
    android:id="@+id/checkbox"
/>
<TextView
    android:inputType="text"
    android:paddingRight="8dp"
    android:layout_width="wrap_content"
    android:focusable="false"
    android:layout_marginTop="6dp"
    android:textSize="16sp"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/checkbox"
    android:layout_toEndOf="@+id/checkbox"
    android:id="@+id/list_text"/>
 <ImageButton
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/del"
     android:src="@android:drawable/ic_menu_delete"
     android:layout_marginTop="3dp"
     android:background="@null"
     android:visibility="gone"
     android:layout_marginRight="8dp"
     android:layout_marginEnd="8dp"
     android:layout_alignParentRight="true"
     android:layout_alignParentEnd="true"/>
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/checkbox"
    android:layout_alignBottom="@+id/checkbox"
    android:layout_centerHorizontal="true"
      android:layout_marginTop="2dp"
      android:orientation="horizontal"
      android:id="@+id/relativeLayout">

    <Button
        android:id="@+id/plus"
        android:layout_width="20dp"

        android:layout_height="20dp"
        android:layout_centerVertical="true"

        android:background="@drawable/round_button"
        android:clickable="true"
        android:focusable="false"
        android:text="+" />

    <TextView
        android:id="@+id/quantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_toEndOf="@+id/plus"
        android:layout_toRightOf="@+id/plus" />

    <Button
        android:id="@+id/minus"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_toEndOf="@+id/quantity"
        android:layout_toRightOf="@+id/quantity"
        android:background="@drawable/round_button"
        android:clickable="true"
        android:focusable="false"
        android:text="-" />

</RelativeLayout>

<RelativeLayout
    android:id="@+id/linearfaq"
    android:layout_marginTop="35dp"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="@color/back"
    android:orientation="vertical" >

</RelativeLayout>

<RelativeLayout
    android:id="@+id/padder"
    android:layout_width="match_parent"
    android:background="@color/blue"
     android:visibility="gone"
    android:layout_height="2dp"

    android:layout_alignTop="@+id/linearfaq"
    >


</RelativeLayout>

类别布局:

<?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"
  android:orientation="horizontal"
  android:background="@drawable/parent_border">
   <TextView
     android:layout_marginTop="10dp"
     android:layout_marginBottom="10dp"
     android:layout_alignParentLeft="true"
     android:layout_alignParentStart="true"
     android:layout_marginLeft="30dp"
     android:layout_marginStart="30dp"
     android:focusable="false"
     android:textSize="20sp"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/cat_text"/>

1 个答案:

答案 0 :(得分:1)

我将所有视图放在一个ConstraintLayout中。通过将背景附加到父级左侧和右侧,视图默认居中。通过左右添加边距,可以实现较小的视图。

就像这个例子一样:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="?attr/actionBarSize"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:buttonTint="@android:color/background_light"
        android:text="CheckBox"
        android:textColor="@android:color/background_light"
        app:layout_constraintBottom_toBottomOf="@+id/imageView2"
        app:layout_constraintStart_toStartOf="@+id/imageView2"
        app:layout_constraintTop_toTopOf="@+id/imageView2" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="8dp"
        android:text="Stuff"
        android:textColor="@android:color/background_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/checkBox"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

看起来像这样:

Screenshot

  

机器人:layout_width = “0dp”

实际上意味着视图将被拉伸以匹配约束。有点像“match_parent”,但是在你通过约束设置的范围内。

稍微使用ConstraintLayout。一旦掌握了它,你就不会再错过了它:)