我正在尝试调整ViewPager的大小取决于设备的屏幕大小。我的ViewPager位于屏幕中心,宽度仅为300dp。我希望我的ViewPager高度调整大小取决于屏幕大小。在我的ViewPager下面有一个ImageView,这个ImageView覆盖了我的ViewPager的一部分。
在我的activity_main.xml
中<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_below="@+id/img_memtext"
android:layout_centerHorizontal="true"/>
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/indicator"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/pager"
android:layout_centerHorizontal="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@drawable/notification_bg"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/attention"
android:layout_below="@id/indicator"/>
在我的viewpager_item.xml
中<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:background="#ffffffff"
android:isScrollContainer="true"
android:fillViewport="true"
android:layout_gravity="center"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
>
<ImageView
android:layout_width="280dp"
android:layout_height="100dp"
android:id="@+id/news_image"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/news_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textStyle="bold"
android:textSize="18dp"
android:layout_below="@+id/news_image"/>
<TextView
android:id="@+id/space"
android:layout_width="20dp"
android:layout_height="20dp"
android:gravity="center"
android:layout_below="@+id/news_title"
/>
<TextView
android:id="@+id/news_body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/news_title"
android:textSize="14dp"/>
<TextView
android:id="@+id/read_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/news_body"
android:textColor="#ff28c0ff"
android:clickable="true"
android:textSize="14dp"/>
</RelativeLayout>
</ScrollView>
有人可以帮我吗?非常感谢你。
答案 0 :(得分:1)
使用具有match_parent高度和宽度的LinearLayout将您希望与ViewPager位于同一行或列中的ViewPager和其他视图包装起来。删除显式维度,然后将layout_weight属性添加到LinearLayout包装的Views中。 More information on layout_weight in LinearLayout
如果您想支持大范围的屏幕尺寸和分辨率,建议您阅读this article,并深入了解android developers website有关最佳做法和说明的信息,以使您的应用能够在(几乎)所有屏幕尺寸。
答案 1 :(得分:0)
我做了一些课程来调整视图的宽度和高度。我不喜欢Dp单位。对我来说很难,每台设备都适合。
这是我班级的一部分:
public class SetViewScale {
private static SetViewScale instance;
public int height, width; //device height, width
public final static int Full_w = 720; // base on design
public final static int Full_h = 1230; //base on design
private SetViewScale(Activity a){
DisplayMetrics displayMetrics = new DisplayMetrics();
a.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int deviceWidth = displayMetrics.widthPixels;
int deviceHeight = displayMetrics.heightPixels;
this.width = deviceWidth;
this.height = deviceHeight;
}
private SetViewScale(WindowManager wm){
DisplayMetrics displayMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displayMetrics);
int deviceWidth = displayMetrics.widthPixels;
int deviceHeight = displayMetrics.heightPixels;
this.width = deviceWidth;
this.height = deviceHeight;
}
public synchronized static SetViewScale getInstance(Activity a){
if(instance == null)
instance = new SetViewScale(a);
return instance;
}
public synchronized static SetViewScale getInstance(WindowManager wm){
if(instance == null)
instance = new SetViewScale(wm);
return instance;
}
public synchronized static SetViewScale getInstance(){
if(instance == null) {
WindowManager wm = (WindowManager) AppManager.context.getSystemService(Context.WINDOW_SERVICE);
instance = new SetViewScale(wm);
}
return instance;
}
/**
* view resizing device width height
* @param view
* @param w
* @param h
* @return
*/
public View setViewScale(View view, int w, int h) {
int setW, setH;
int x = width * w;
setW = (int)(x/Full_w);
int y = h*setW;
setH = (int)(y/w);
ViewParent parent = view.getParent();
if(parent instanceof LinearLayout){
LinearLayout.LayoutParams lp = (LayoutParams) view.getLayoutParams();
lp.width = setW;
lp.height = setH;
view.setLayoutParams(lp);
}else if(parent instanceof RelativeLayout){
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)view.getLayoutParams();
lp.width = setW;
lp.height = setH;
view.setLayoutParams(lp);
}else if(parent instanceof FrameLayout){
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)view.getLayoutParams();
lp.width = setW;
lp.height = setH;
view.setLayoutParams(lp);
}else if(parent instanceof AbsListView){
AbsListView.LayoutParams lp = (AbsListView.LayoutParams)view.getLayoutParams();
lp.width = width;
lp.height = height;
view.setLayoutParams(lp);
}else{
return null;
}
return view;
}
public View setFillWidthScale(View view, int w, int h) {
int setW, setH;
int x = width * w;
setW = (int)(x/Full_w);
int y = h*setW;
setH = (int)(y/w);
ViewParent parent = view.getParent();
if(parent instanceof LinearLayout){
LinearLayout.LayoutParams lp = (LayoutParams) view.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
lp.height = setH;
view.setLayoutParams(lp);
}else if(parent instanceof RelativeLayout){
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)view.getLayoutParams();
lp.width = RelativeLayout.LayoutParams.MATCH_PARENT;
lp.height = setH;
view.setLayoutParams(lp);
}else if(parent instanceof FrameLayout){
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)view.getLayoutParams();
lp.width = FrameLayout.LayoutParams.MATCH_PARENT;
lp.height = setH;
view.setLayoutParams(lp);
}else if(parent instanceof AbsListView){
AbsListView.LayoutParams lp = (AbsListView.LayoutParams)view.getLayoutParams();
lp.width = width;
lp.height = height;
view.setLayoutParams(lp);
}else{
return null;
}
return view;
}
使用px单位。
我希望这对你有所启示。