Android inflate与xml根元素的布局参数不起作用的问题

使用inflate加载布局,根布局的布局参数不起作用,如下Fragment中加载fragment_layout

public class MyFragment extends Fragment{
    private View         mView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_layout, null);
        return mView;
    }
}

预期效果是

 

###############################实际布局与对应的效果1################################################

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:background="#00f"
              android:layout_width="200dp"
              android:layout_height="match_parent">
    <LinearLayout
        android:background="#0f0"
        android:layout_width="100dp"
        android:layout_height="match_parent">

    </LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:background="#00f"
              android:layout_width="200dp"
              android:layout_height="match_parent">
    <RelativeLayout
        android:background="#0f0"
        android:layout_width="100dp"
        android:layout_height="match_parent">

    </RelativeLayout>

</LinearLayout>

 

###############################实际布局与对应的效果2################################################

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:background="#00f"
              android:layout_width="200dp"
              android:layout_height="match_parent">
    <LinearLayout
        android:background="#0f0"
        android:layout_width="100dp"
        android:layout_height="match_parent">

    </LinearLayout>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:background="#00f"
              android:layout_width="200dp"
              android:layout_height="match_parent">
    <RelativeLayout
        android:background="#0f0"
        android:layout_width="100dp"
        android:layout_height="match_parent">

    </RelativeLayout>

</RelativeLayout>

 

###############################实际布局与对应的效果3################################################

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:background="#00f"
              android:layout_width="200dp"
              android:layout_height="match_parent">
    <LinearLayout
        android:background="#0f0"
        android:layout_width="100dp"
        android:layout_height="match_parent">

    </LinearLayout>

    <!--把高度撑起来-->
    <View
        android:background="#f00"
        android:layout_width="2dp"
        android:layout_height="match_parent"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:background="#00f"
              android:layout_width="200dp"
              android:layout_height="match_parent">
    <RelativeLayout
        android:background="#0f0"
        android:layout_width="100dp"
        android:layout_height="match_parent">

    </RelativeLayout>

    <!--把高度撑起来-->
    <View
        android:background="#f00"
        android:layout_width="2dp"
        android:layout_height="match_parent"/>

</LinearLayout>

 

###############################结论################################################

1.根布局无论是LinearLayout还是RelativeLayout,布局参数都不起作用,否则应有宽度为200dp的蓝色背景

2.根布局为LinearLayout时,无论下一层布局是LinearLayout或RelativeLayout,下一层布局的布局参数都不起作用

3.根布局为RelativeLayout时,无论下一层布局是LinearLayout或RelativeLayout,下一层布局参数都能起作用

4.根布局为LinearLayout时,下一层布局的布局参数都不起作用,此时可添加一个View把高度撑起来,把View背景设置为透明即不影响UI效果

 

相关资料:

Android Layout的layout_height等属性为什么有时会不起作用?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值