Android GridView设置标题视图高度

Android GridView设置标题视图高度,android,android-layout,android-viewpager,height,android-gridview,Android,Android Layout,Android Viewpager,Height,Android Gridview,我正在实现一个带有标题视图的GridView() . 我希望HeaderView占据屏幕高度的1/3。问题是在header\u view.xml中,我必须设置布局高度值,否则视图不可见 以下是我的活动: public class TestGVHeader extends Activity { Context context = this; private GridViewWithHeaderAndFooter gridView; private Produc

我正在实现一个带有标题视图的GridView() . 我希望HeaderView占据屏幕高度的1/3。问题是在
header\u view.xml
中,我必须设置
布局高度
值,否则视图不可见

以下是我的活动:

    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而不是固定值吗?
谢谢。

使用标题视图的自定义
视图组
很容易,在您的情况下,它是
框架布局
。您只需覆盖测量方法,并将高度计算为屏幕高度的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);
    }
}
然后将其用作
标题\u视图
根目录。请注意,我们将其高度设置为
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>


试试
android:weight=“0.65”
你的意思是
android:layout\u weight=“0.65”
?如果我设置
android:layout\u weight=“0.65”
android:layout\u height=“0dp”
ViewPager
不可见:(设置
android:layout\u height=“wrap\u content”
android:layou weight=“0.06”
不工作。如果我设置
android:layout\u height=“wrap\u content”
android:layout\u height=“wrap\u height=“匹配你的父母"
ViewPager
不再可见,无论我在
android:layout\u weight
中设置了什么,它看起来都会工作。在我的例子中,主要的基础是片段&它是一个容纳ViewPager的线性布局,然后我在GridLayout中使用这个GridViewWith Header和Footer将其添加为headerView。一切都如期而至但是如果我们有更多的项目&我们会快速上下滚动。然后它会在onMeasure()的最后一行崩溃。
super.onMeasure(widthSpec,oneThreeHeight)因此,如果你有一个
LinearLayout
作为标题视图的根视图,那么你需要创建一个
OneThreeScreenLinearLayout扩展LinearLayout
并在
onMeasure
方法中做同样的事情。然后用它代替
LinearLayout
。好的。让我试试。如果它起作用,我会很高兴的。我不需要吗d在任何地方传递
oneThreeHeight
值?必须这样做。测量
onMeasure
方法的实现将是相同的