这是我的一个片段的屏幕截图(设备:Xiaomi Redmi 2)
该片段分为4个部分:
这是相应的XML布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:orientation="vertical"
android:weightSum="1.5">
<!-- for displaying ads -->
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
<LinearLayout
android:id="@+id/llDots"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="0.5"
android:gravity="center"
android:background="@android:color/black"
android:orientation="horizontal" />
</LinearLayout>
<!-- for displaying news -->
<android.support.v7.widget.RecyclerView
android:id="@+id/news_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5" />
<!-- bottom navigation -->
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
我认为ViewPager指标和AHBottomView的高度是正确的。但是ViewPager的高度太高了。我想要的是ViewPager的高度以适应图像广告的高度,并且RecyclerView的高度更大。我尝试将值weightSum(顶部LinearLayout)从3减少到2.5,结果显示AHbottomNavigation不再可见。
答案 0 :(得分:1)
如下所示改变
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
>
<!-- for displaying ads -->
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:id="@+id/llDots"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:background="@android:color/black"
android:orientation="horizontal" />
<!-- for displaying news -->
<android.support.v7.widget.RecyclerView
android:id="@+id/news_list"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- bottom navigation -->
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
您可以使用以下代码。
public class WrapContentViewPager extends ViewPager {
private int mCurrentPagePosition = 0;
public WrapContentViewPager(Context context) {
super(context);
}
public WrapContentViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
View child = getChildAt(mCurrentPagePosition);
if (child != null) {
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = child.getMeasuredHeight();
heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
}
} catch (Exception e) {
e.printStackTrace();
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void reMeasureCurrentPage(int position) {
mCurrentPagePosition = position;
requestLayout();
}
}
在xml:
中声明它<your.package.name.WrapContentViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</your.package.name.WrapContentViewPager>
并在页面滑动上调用reMeasureCurrentpage
功能。
final WrapContentViewPager wrapContentViewPager = (WrapContentViewPager) findViewById(R.id.view_pager);
wrapContentViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
wrapContentViewPager.reMeasureCurrentPage(wrapContentViewPager.getCurrentItem());
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
快乐的编码:)