我正在实现一个带有标题视图的GridView(GridViewWithHeaderAndFooter)
。我希望HeaderView占据屏幕高度的1/3。问题是在header_view.xml
中我必须设置layout_height
值,否则视图不可见。
这是我的活动:
public class TestGVHeader extends Activity {
Context context = this;
private GridViewWithHeaderAndFooter gridView;
private ProductsGridViewAdapter gridViewAdapter;
private ViewPager viewPager;
private PagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_gvheader);
gridView = (GridViewWithHeaderAndFooter) findViewById(R.id.gridView);
LayoutInflater layoutInflater = LayoutInflater.from(this);
View headerView = layoutInflater.inflate(R.layout.test_header_view, null);
gridView.addHeaderView(headerView);
gridViewAdapter = new ProductsGridViewAdapter(this, getProducts());
gridView.setAdapter(gridViewAdapter);
viewPager = (ViewPager) headerView.findViewById(R.id.viewpager);
viewPagerAdapter = new CataloguePagerAdapter(context, this, this.getCatalogues());
viewPager.setAdapter(viewPagerAdapter);
}
}
活动布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<in.srain.cube.views.GridViewWithHeaderAndFooter
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="1sp"
android:numColumns="5"
android:stretchMode="columnWidth"
android:verticalSpacing="1sp"
android:background="@android:color/white">
</in.srain.cube.views.GridViewWithHeaderAndFooter>
和标题视图布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="450dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:background="@android:color/white"/>
</FrameLayout>
有人知道如何将android:layout_height="450dp"
设置为屏幕高度的1/3而不是固定值吗?
感谢。
答案 0 :(得分:2)
使用标题视图的自定义ViewGroup
可以轻松完成,在您的情况下FrameLayout
。您只需覆盖onMeasure
方法并将高度计算为屏幕高度的1/3。因此,首先,在项目中创建下一个自定义视图类:
public class OneThreeScreenFrameLayout extends FrameLayout {
public OneThreeScreenFrameLayout(Context context) {
super(context);
}
public OneThreeScreenFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public OneThreeScreenFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
int screenHeight = getResources().getDisplayMetrics().heightPixels;
int oneThreeHeight = MeasureSpec.makeMeasureSpec(screenHeight / 3,
MeasureSpec.EXACTLY);
super.onMeasure(widthSpec, oneThreeHeight);
}
}
然后将其用作header_view
根。请注意,我们将其高度设置为0dp
,因为我们无论如何都要更改它:
<?xml version="1.0" encoding="utf-8"?>
<your.package.name.OneThreeScreenFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:gravity="center"
android:background="@android:color/white"/>
</your.package.name.OneThreeScreenFrameLayout>
那就是它!您的标题视图现在是屏幕高度的1/3。示例如下: