我已经设置了一个简单的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?
我有一个DialogFragment包含RecyclerView(卡片列表).
其中RecyclerView一个或多个CardViews可以具有任何高度.
我想DialogFragment根据CardViews其中包含的内容给出正确的高度.
通常情况下,这将是简单的,我会成立wrap_content的RecyclerView这个样子.
<android.support.v7.widget.RecyclerView ...
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:scrollbars="vertical" >
</android.support.v7.widget.RecyclerView>
Run Code Online (Sandbox Code Playgroud)
因为我使用的RecyclerView这个不起作用,请看:
https://issuetracker.google.com/issues/37001674
和
在这两个页面上,人们建议扩展LinearLayoutManager和覆盖onMeasure()
我首先使用了第一个链接中提供的LayoutManager:
public static class WrappingLayoutManager extends LinearLayoutManager {
public WrappingLayoutManager(Context context) {
super(context);
}
private int[] mMeasuredDimension = new int[2];
@Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state,
int widthSpec, int heightSpec) {
final int widthMode = View.MeasureSpec.getMode(widthSpec);
final …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments android-studio android-recyclerview