动态设置布局高度

lyyzms 2018-05-07 12:12:05
动态设置父布局高度后,子控件高度更改,父布局没有变化。请问是什么问题?
...全文
1084 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jing丶無雙 2018-05-08
  • 打赏
  • 举报
回复
我把你提供的XML做了背景颜色的区分:#ffffff为白色,#9370DB为紫色,#0000EE为蓝色
<?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高宽进行了设置,界面效果也与你代码逻辑相符合,所以不大明白你现在想要什么样的效果
lyyzms 2018-05-07
  • 打赏
  • 举报
回复

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>
Jing丶無雙 2018-05-07
  • 打赏
  • 举报
回复
贴下代码喃

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧