使用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效果
相关资料: