从两个布局重建为一个布局并移动所有子视图

时间:2016-07-07 08:01:30

标签: android android-layout

我在一个垂直的LinearLayout

中有两个水平线LinearLayouts

在每个水平LinearLayout 中,有2 DealsTabletButtons

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">

                <com.app.views.deals.DealsTabletButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    app:d_icon="@drawable/deals_icon_red"
                    app:d_lower_text="@string/all_deals_right_active_now"
                    app:d_text="@string/deals"
                    app:d_upper_text="@string/view" />

                <com.app.views.deals.DealsTabletButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginLeft="2dp"
                    android:layout_weight="1"
                    app:d_icon="@drawable/categories_icon_red"
                    app:d_lower_text="@string/over_500_top_categories"
                    app:d_text="@string/category"
                    app:d_upper_text="@string/shop_by" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">

                <com.app.views.deals.DealsTabletButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="2dp"
                    android:layout_weight="1"
                    app:d_icon="@drawable/brands_icon_red"
                    app:d_lower_text="@string/over_2500_popular_brands"
                    app:d_text="@string/brand"
                    app:d_upper_text="@string/shop_by" />

                <com.app.views.deals.DealsTabletButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginLeft="2dp"
                    android:layout_marginTop="2dp"
                    android:layout_weight="1"
                    app:d_is_empty="true" />
            </LinearLayout>
</LinearLayout>

我想在屏幕旋转后重构这些布局以编程方式,并实现类似的目标:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:orientation="horizontal">

            <com.app.views.deals.DealsTabletButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    app:d_icon="@drawable/deals_icon_red"
                    app:d_lower_text="@string/all_deals_right_active_now"
                    app:d_text="@string/deals"
                    app:d_upper_text="@string/view" />

            <com.app.views.deals.DealsTabletButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    app:d_icon="@drawable/categories_icon_red"
                    app:d_lower_text="@string/over_500_top_categories"
                    app:d_text="@string/category"
                    app:d_upper_text="@string/shop_by" />


            <com.app.views.deals.DealsTabletButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    app:d_icon="@drawable/brands_icon_red"
                    app:d_lower_text="@string/over_2500_popular_brands"
                    app:d_text="@string/brand"
                    app:d_upper_text="@string/shop_by" />

            <com.app.views.deals.DealsTabletButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    app:d_is_empty="true" />
</LinearLayout>

为了抓住屏幕旋转,请使用onConfigurationChanged

中的Fragment

是否有可能获得此结果?

1 个答案:

答案 0 :(得分:0)

当然可以,xmls只是如何以编程方式构建视图的说明。

但是你不需要这样做,你可以在onConfigureChanged中调用它:

setContentView(R.layout.landlayout)

你的onConfigChange看起来像这样:

public class YourActivity extends Activity{
 @Override
 public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if(isLandOrientation()){
        setContentView(R.layout.land);
    }else{
        setContentView(R.layout.not_land);   
    }
 }
....
}