我正在实现一个带有HeaderView(GridViewWithHeaderAndFooter)的GridView。我希望HeaderView占据屏幕高度的1/3。问题在于,在header_view.xml中,我必须设置layout_height值,否则该视图将不可见。
以下是我的Activity:
活动布局:
有人知道如何将
以下是我的Activity:
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,而不是固定值吗?谢谢。
android:weight="0.65"
- M Dandroid:layout_weight="0.65"
吗?如果我设置了android:layout_weight="0.65"
和android:layout_height="0dp"
,那么ViewPager
就不可见了 :( - Aleandroid:layout_height =“wrap_content”
和android:layout_weight =“0.06”
设置。 - M Dandroid:layout_height="wrap_content"
或者android:layout_height="match_parent"
,ViewPager
就不可见了,无论我在android:layout_weight
中设置什么。 - Ale