80,337
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<android.support.v4.view.ViewPager
android:id="@+id/vp_lunbo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#9370DB" />
<LinearLayout
android:id="@+id/layout_dot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/vp_lunbo"
android:layout_centerHorizontal="true"
android:background="#0000EE"
android:orientation="horizontal"
android:paddingBottom="5dp" />
</RelativeLayout>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager view = (ViewPager) findViewById(R.id.vp_lunbo);
//这里是你动态设置ViewPager高宽的代码。现在注释这里,整个屏幕显示的是紫色。取消注释,则紫色区域显示等于你给ViewPager设置的高宽
// WindowManager mWm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
// DisplayMetrics metric = new DisplayMetrics();
// mWm.getDefaultDisplay().getMetrics(metric);
// int screenWidth = metric.widthPixels;
// float precentOfScreen = (float) 0.6;
// float hw = (float) 0.5;
// ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
// if (layoutParams != null) {
// layoutParams.width = (int) (precentOfScreen * (float) screenWidth);
// layoutParams.height = (int) ((float) layoutParams.width * hw);
// view.setLayoutParams(layoutParams);
// } else {
// throw new RuntimeException(
// "view。getLayoutParams()=null ,precentOfScreen="
// + precentOfScreen + ",hw=" + hw);
// }
}
}
由此看出你的代码对ViewPager高宽进行了设置,界面效果也与你代码逻辑相符合,所以不大明白你现在想要什么样的效果
WindowManager mWm = (WindowManager) App.getAppContext().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metric = new DisplayMetrics();
mWm.getDefaultDisplay().getMetrics(metric);
int screenWidth = metric.widthPixels;
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
if (layoutParams != null) {
layoutParams.width = (int) (precentOfScreen * (float) screenWidth);
layoutParams.height = (int) ((float) layoutParams.width * hw);
view.setLayoutParams(layoutParams);
} else {
throw new RuntimeException("view。getLayoutParams()=null ,precentOfScreen=" + precentOfScreen + ",hw=" + hw);
}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/vp_lunbo"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/layout_dot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/vp_lunbo"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:paddingBottom="5dp" />
</RelativeLayout>