Java ExpandableListView子项宽度
我的应用程序中有一个可扩展的列表视图。我想使子布局比组标题窄一点,并使其居中。由于一些视觉效果,我必须从代码中设置布局参数,但是我似乎找不到最好的解决方案,因为我尝试过从xml更改布局宽度,但运气不好。任何建议都会有帮助。 谢谢 适配器getChild()方法: 项目布局:Java ExpandableListView子项宽度,java,android,expandablelistview,Java,Android,Expandablelistview,我的应用程序中有一个可扩展的列表视图。我想使子布局比组标题窄一点,并使其居中。由于一些视觉效果,我必须从代码中设置布局参数,但是我似乎找不到最好的解决方案,因为我尝试过从xml更改布局宽度,但运气不好。任何建议都会有帮助。 谢谢 适配器getChild()方法: 项目布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="match_parent" andro
<?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"/>
我会将所有视图放在一个约束窗口中。通过将背景附着到父对象的左侧和右侧,视图默认居中。通过向左和向右添加边距,可以使视图变小 如本例所示:
<?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>
android:layout_width=“0dp”
实际上意味着视图将被拉伸以匹配约束。有点像“match_parent”,但在通过其约束设置的范围内
玩一玩这个游戏。一旦掌握了窍门,您就不想再错过它了:)您是否尝试过使用constraintlayout作为基本布局而不是relativelayout?根据我的经验,对于你试图完成的事情来说,使用它更容易。我将尝试调整contraint布局,但你能指定实现中心视图的任何说明吗?我尝试过,但它不起作用。我认为问题与ConvertView充气机有关您现在在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>