目前我正在滚动视图中开发设置菜单。基本结构如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollview_settings"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/paddingSmall">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent_white_settings"
android:paddingTop="@dimen/paddingMedium"
android:paddingBottom="@dimen/paddingMedium">
<ImageView
android:id="@+id/logo"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:src="@drawable/newspaper_logo_color"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/paddingBig"
android:adjustViewBounds="true"/>
</RelativeLayout>
<!-- Layout News Uebersicht -->
<include layout="@layout/separator_line_grey"/>
<de.basecom.noznewsapp.views.FontTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="@color/buttonHoverBackground"
android:textSize="@dimen/sliding_image_kicker_font_size"
android:paddingLeft="@dimen/paddingLarge"
android:paddingTop="@dimen/paddingMedium"
android:paddingBottom="@dimen/paddingMedium"
android:text="@string/news_overview"
android:textScaleX="@dimen/letter_spacing"
android:textStyle="bold"
android:gravity="center_vertical"
android:background="@color/settings_header_color"
android:layout_gravity="center_vertical"/>
<include layout="@layout/separator_line_grey"/>
<!-- News-Overview Issues -->
<LinearLayout
android:id="@+id/issue_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/transparent_white_settings">
<de.basecom.noznewsapp.views.NewsScrollListView
android:id="@+id/listview_issues"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:divider="@null"
android:dividerHeight="0dp" />
</LinearLayout>
<!-- Layout Einstellungen -->
<de.basecom.noznewsapp.views.FontTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="@color/buttonHoverBackground"
android:textSize="@dimen/sliding_image_kicker_font_size"
android:paddingLeft="@dimen/paddingLarge"
android:paddingTop="@dimen/paddingMedium"
android:paddingBottom="@dimen/paddingMedium"
android:text="@string/settings"
android:textStyle="bold"
android:textScaleX="@dimen/letter_spacing"
android:gravity="center_vertical"
android:background="@color/settings_header_color"
android:layout_gravity="center_vertical"/>
<include layout="@layout/separator_line_grey"/>
<!-- Einstellungs- Werte -->
<LinearLayout
android:id="@+id/settings_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/transparent_white_settings">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/paddingLarge">
<de.basecom.noznewsapp.views.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/receive_pushmessages"
android:textAllCaps="true"
android:textScaleX="@dimen/letter_spacing"
android:textSize="@dimen/sliding_image_kicker_font_size"
android:layout_centerVertical="true"
android:textColor="@color/buttonBackground"/>
<Switch
android:id="@+id/receive_pushmessages_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/empty_string"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
对于列表视图,我使用了自定义实现,给它一个固定高度并使其能够在滚动视图中滚动(注意:我必须在滚动视图中使用列表视图,因为我有很多元素,所以使用线性布局不会不好)。
public class CustomListView extends ListView {
public CustomListView(Context context) {
super(context);
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListView(Context context, AttributeSet attrs,int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
列表视图的每一项都包含一个标签和一个网格视图,它们都显示正确。但现在我想实现功能,用户可以点击标签来显示和隐藏gridview的可见性。
但是,如果我单击标签并显示网格视图,则列表视图的高度不会适应新的打开元素。它始终保持在相同的高度,如果我打开一个元素,下面的元素将不再可见。如果我调用requestLayout()
listview 的方法再次触发它,这也会发生onMeasure
。
有人知道我做错了什么吗?
编辑:更新了我的 xml 文件:)