在LinearLayout

时间:2018-12-19 09:08:22

标签: android android-orientation

我在底部有一个自定义视图,其中包含纵向模式下的图标。我希望此视图在横向模式下位于屏幕的右侧。我试图在运行时设置属性,但无法正常工作。我确定我丢失了某些东西或做错了什么。

下面是我的XML:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.HomeActivity">

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/title_mirror"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent">
        </FrameLayout>



         <LinearLayout
            android:id="@+id/navigation_view"
            android:layout_width="match_parent"
        android:layout_height="56dp"
            android:layout_alignParentBottom="true"
            android:background="?android:attr/windowBackground"
            android:orientation="horizontal"
            android:weightSum="1">

                <ImageView
                    android:id="@+id/image_mirror"
                    android:layout_width="@dimen/_20sdp"
                    android:layout_height="@dimen/_20sdp"
                    android:layout_gravity="center"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="8dp"
                    android:layout_weight="0.33"
                    app:srcCompat="@drawable/ic_mirror_not_selected" />

                <ImageView
                    android:id="@+id/image_review"
                    android:layout_width="@dimen/_20sdp"
                    android:layout_height="@dimen/_20sdp"
                    android:layout_gravity="center"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="8dp"
                    android:layout_weight="0.33"
                    app:srcCompat="@drawable/ic_review_not_selected" />

                <ImageView
                    android:id="@+id/img_profile"
                    android:layout_width="@dimen/_20sdp"
                    android:layout_height="@dimen/_20sdp"
                    android:layout_gravity="center"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="8dp"
                    android:layout_weight="0.33"
                    app:srcCompat="@drawable/ic_profile_not_selected" />

            </LinearLayout>

        </RelativeLayout>

在上面ID为navigation_view的XML LinearLayout中,这是我的自定义视图。在横向模式下,我希望此视图在屏幕的右侧。我试图在运行时设置以下属性,但这将视图保持在顶部而不是右侧。

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

        ViewGroup.LayoutParams params1 = view_nav.getLayoutParams();

        params1.height = ViewGroup.LayoutParams.MATCH_PARENT;
        params1.width = 100;
        params.addRule(RelativeLayout.ALIGN_PARENT_END);

        LinearLayout layout = new LinearLayout(this);

        layout.setOrientation(LinearLayout.VERTICAL);
        view_nav.setLayoutParams(params);

        img_mirror.startAnimation(toLandAnim);
        img_review.startAnimation(toLandAnim);
        img_profile.startAnimation(toLandAnim);


        //  constraintSet.connect(view_nav.getId(), ConstraintSet.LEFT, R.id.fragment_container, ConstraintSet.LEFT, 0);
        //  constraintSet.applyTo(constraintLayout);
    } else {
        // In portrait

        img_mirror.startAnimation(toPortAnim);
        img_review.startAnimation(toPortAnim);
        img_profile.startAnimation(toPortAnim);
    }
}

我不确定在父级为RelativeLayout的运行时如何设置LinearLayout的属性。

1 个答案:

答案 0 :(得分:0)

创建另一个包含景观布局的xml文件。此xml文件的名称应与纵向文件的名称相同,并且应位于res/layout-land中(如果纵向布局位于res/layout文件夹中)。 Android将根据设备的方向使用正确的布局,并且您将仅根据提供的代码示例处理动画。

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

    img_mirror.startAnimation(toLandAnim);
    img_review.startAnimation(toLandAnim);
    img_profile.startAnimation(toLandAnim);

} else {
    // In portrait

    img_mirror.startAnimation(toPortAnim);
    img_review.startAnimation(toPortAnim);
    img_profile.startAnimation(toPortAnim);
}

`

有关更多信息:https://developer.android.com/training/multiscreen/screensizes