gpt4 book ai didi

java - ExpandableListView 子级宽度

转载 作者:行者123 更新时间:2023-12-02 11:33:21 31 4
gpt4 key购买 nike

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

适配器 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"/>

最佳答案

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

就像这个例子:

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

android:layout_width="0dp"

实际上意味着 View 将被拉伸(stretch)以匹配约束。有点像“match_parent”,但在您通过其约束设置的范围内。

稍微尝试一下 ConstraintLayout。一旦你掌握了它,你就不会想再错过它了:)

关于java - ExpandableListView 子级宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49120036/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com