如何根据屏幕大小调整 ViewPager 的大小?
How can I resize my ViewPager depends on the size of my screen?
我正在尝试根据设备的屏幕尺寸调整 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>
有人可以帮我吗?非常感谢。
使用具有 match_parent 高度和宽度的 LinearLayout 来包装您的 ViewPager 和您希望与 ViewPager 位于同一行或列中的其他视图。删除显式尺寸,然后将 layout_weight 属性 添加到由 LinearLayout 包装的视图。 More information on layout_weight in LinearLayout.
如果您想支持各种屏幕尺寸和分辨率,我建议您阅读 this article, and look deeper in the android developers website 以获得最佳实践和说明,以使您的应用在(几乎)所有屏幕尺寸上都能正常运行。
我做了一些 class 来调整视图的宽度和高度。我不喜欢 Dp 单位。对我来说很难,每个设备都适合。
这里是我的部分 class :
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 单位。
希望对你有所提示。
我正在尝试根据设备的屏幕尺寸调整 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>
有人可以帮我吗?非常感谢。
使用具有 match_parent 高度和宽度的 LinearLayout 来包装您的 ViewPager 和您希望与 ViewPager 位于同一行或列中的其他视图。删除显式尺寸,然后将 layout_weight 属性 添加到由 LinearLayout 包装的视图。 More information on layout_weight in LinearLayout.
如果您想支持各种屏幕尺寸和分辨率,我建议您阅读 this article, and look deeper in the android developers website 以获得最佳实践和说明,以使您的应用在(几乎)所有屏幕尺寸上都能正常运行。
我做了一些 class 来调整视图的宽度和高度。我不喜欢 Dp 单位。对我来说很难,每个设备都适合。
这里是我的部分 class :
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 单位。
希望对你有所提示。