我已经设置了一个简单的ViewPager,每页都有一个高度为200dp的ImageView.
这是我的寻呼机:
pager = new ViewPager(this);
pager.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
pager.setBackgroundColor(Color.WHITE);
pager.setOnPageChangeListener(listener);
layout.addView(pager);
Run Code Online (Sandbox Code Playgroud)
尽管将高度设置为wrap_content,但即使imageview仅为200dp,寻呼机也会填充屏幕.我试图用"200"替换寻呼机的高度,但这给了我多种分辨率的不同结果.我无法将"dp"添加到该值.如何在寻呼机的布局中添加200dp?
在我的xml文件中,我有一个线性布局,其中包含用于显示图像的ViewPager和另一个包含用于选择图像的上一个和下一个按钮的线性布局.我的xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/goto_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first" >
</Button>
<Button
android:id="@+id/goto_last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="last" >
</Button>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的问题是ViewPager正在全屏显示,并且带有前一个下一个按钮的Linearlayout没有显示,因为没有空间来绘制此LinearLayout.
我正在使用Fragments来为ViewPager填充视图.进入ViewPager的片段视图的xml文件是:
<ImageView
android:id="@+id/ItemImage"
android:layout_width="320dp"
android:layout_height="180dp" />
<TextView
android:id="@+id/ItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:layout_marginBottom="5dp"
android:textColor="#ffffffff"
android:textStyle="bold" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
渲染后我得到的输出是这样的:
1.image(覆盖45%的屏幕高度)2.
空白区域(覆盖45%的屏幕高度)
3.Textview(覆盖10%屏幕高度的其余部分)
我想要的输出是:
1.image(覆盖45%的屏幕高度)
2.Textview(覆盖10%的屏幕高度)
3.LinarLayout for Buttons(覆盖45%屏幕高度的其余部分)