相对布局权重

Relative layout weight

RelativeLayout 的情况下,我需要找到线性布局权重参数的替代品。

下面是 LinearLayout

的代码
LinearLayout.LayoutParams fp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,0);
fp.weight = 2;
FrameLayout parent_frame = (FrameLayout) findViewById(R.id.frame_container);
parent_frame.setLayoutParams(fp);

LinearLayout.LayoutParams tp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,0);
tp.weight = 1;
FrameLayout popup_layout = (FrameLayout) findViewById(R.id.popup_layout_id);
popup_layout.setLayoutParams(tp);

如果父布局更改为 RelativeLayout,我需要做同样的事情, 我有下面的代码,但输出是黑屏。

   RelativeLayout.LayoutParams fp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,0);
    //fp.weight = 2;

    FrameLayout parent_frame = (FrameLayout) findViewById(R.id.frame_container);
    parent_frame.setLayoutParams(fp);

    RelativeLayout.LayoutParams tp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,0);
    //tp.weight = 1;

    FrameLayout popup_layout = (FrameLayout) findViewById(R.id.popup_layout_id);
    popup_layout.setLayoutParams(tp);

基本上我需要的是垂直显示两个布局,第一个布局占屏幕的 70%,第二个布局占屏幕的 30%,而且我不能在 xml 中使用 LinearLayout,因为它需要大量代码更改。

新的 PercentRelativeLayout 是你的朋友:

https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html