1. 程式人生 > >Android inflate與xml根元素的佈局引數不起作用的問題

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效果

相關資料: