在这里,我使用视图和代码,如下所示。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableRow
android:id="@+id/tab_stone_info"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="1dp"
android:background="@color/styleBreakTabColor"
android:weightSum="9">
<TextView
android:id="@+id/stone_Type"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/Type"
android:textColor="@color/txtStyleBreakup"
android:textSize="12sp" />
<TextView
android:id="@+id/stone_Shape"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/Shape"
android:textColor="@color/txtStyleBreakup"
android:textSize="12sp" />
<TextView
android:id="@+id/stone_Qlty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/Quality"
android:textColor="@color/txtStyleBreakup"
android:textSize="12sp" />
<TextView
android:id="@+id/stone_Grade"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/Grade"
android:textColor="@color/txtStyleBreakup"
android:textSize="12sp" />
<TextView
android:id="@+id/stone_Qty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/Qty"
android:textColor="@color/txtStyleBreakup"
android:textSize="12sp" />
<TextView
android:id="@+id/stone_Wt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/Wt"
android:textColor="@color/txtStyleBreakup"
android:textSize="12sp" />
<TextView
android:id="@+id/stone_MMSize"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/MMSize"
android:textColor="@color/txtStyleBreakup"
android:textSize="12sp" />
<TextView
android:id="@+id/stone_Setting"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/Setting"
android:textColor="@color/txtStyleBreakup"
android:textSize="12sp" />
<TextView
android:id="@+id/stone_SettMode"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/cell_shape"
android:gravity="center"
android:text="@string/SettingMode"
android:textColor="@color/txtStyleBreakup"
android:textSize="12sp" />
</TableRow>
<RelativeLayout
android:id="@+id/layout_tab_stone_info_value"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.mindtech.ExpandableHeightListView
android:id="@+id/listviewstone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#00000000"
android:gravity="center"
android:horizontalSpacing="2dp"
android:isScrollContainer="false"
android:stretchMode="columnWidth"
android:verticalSpacing="20dp"
/>
</RelativeLayout>
</LinearLayout>
我使用epandablelistview作为下面的类。
public class ExpandableHeightListView extends ListView
{
boolean expanded = false;
public ExpandableHeightListView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public ExpandableHeightListView(Context context)
{
super(context);
}
public ExpandableHeightListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public boolean isExpanded()
{
return expanded;
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
// MeasureSpec.AT_MOST);
//
// super.onMeasure(widthMeasureSpec, expandSpec);
// int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
// Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
// super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
// ViewGroup.LayoutParams params = getLayoutParams();
// params.height = getMeasuredHeight();
if (isExpanded())
{
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
我提到这个问题,但没有弄清楚。 Grid of images inside ScrollView
当第一行高度不是第二行或其他行时,会出现主要问题。如果所有行内容长度相同则没有问题。
答案 0 :(得分:1)
看起来你正在为ListView获取LayoutParams,修改height参数,但是你永远不会调用setLayoutParams来告诉视图使用修改后的高度。试试这个:
if (isExpanded()) {
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
//Add this line
setLayoutParams(params);
}
另外,我在过去使用onMeasure()方法遇到了问题,而不是在框架调用该方法时测量的一切。您可以尝试ViewTreeObserver,在这里您可以注册一个监听器,以便在完整视图布局时得到通知,如下所示:
final LinearLayout layout = (LinearLayout) findViewById(R.id.layout_id);
ViewTreeObserver vto;
if (layout != null) {
vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//Modify your LayoutParams here
}
});
}
答案 1 :(得分:0)
如果只有一个孩子,那么最好使用不同ExpandableListView
的{{1}}或RecyclerView
。 Here是一个关于使用layouts
的完整且非常好的教程。
使用ExpandableListView
时,它总是会增加一个比原来高一点的高度(或者可能是另一个案例,但我不知道为什么。如果有人知道,请善意纠正我)。所以你必须在最后手动添加一些ListView
高度。
我是通过覆盖dp's
方法完成的。以下是我的表现。
onDraw
答案 2 :(得分:0)
您正在修改高度,但您没有将其传递给视图。
使用此方法setLayoutParams(params);