0

我有 RTL 布局和 LTR 布局。

一开始一切都很好,但在进入警报模式后

两种布局都左对齐。

我怎样才能解决这个问题?或者至少更好地检查情况?

是否有任何工具可以让我在我的活动视图中查看当前布局属性和属性?该代码更改了原始 XMl 的许多属性,并且在特定情况下很难遵循。

这是我的xml:

 <LinearLayout
            android:id="@+id/navBarBox2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="5dp"
            android:padding="0dp"
            android:orientation="vertical" >
            <LinearLayout
            android:id="@+id/navBarDistanceLayout"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="3dp"
            android:layout_marginTop="5dp"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:visibility="gone">

            <MyTextView
                android:id="@+id/navBarDistance"
                style="@style/NavBarDistance"
                android:textSize="19dp"
                android:text="miles"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

            <MyTextView
                android:id="@+id/navBarDistanceUnit"
                style="@style/NavBarDistUnit"
                android:textSize="19dp"
                android:text="0.1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="3dp" 
                />
        </LinearLayout>
            <LinearLayout
                android:id="@+id/navBarDistanceLayoutRtl"
                android:layout_width="70dp"
                android:layout_height="match_parent"
                android:layout_marginRight="8dp"
                android:layout_marginTop="5dp"
                android:gravity="center_vertical|right"
                android:orientation="horizontal"
                android:visibility="gone">

                <MyTextViewType
                    android:id="@+id/navBarDistanceUnitRtl"
                    style="@style/NavBarDistUnit"
                    android:textSize="19dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="3dp"
                    />

                <MyTextViewType
                    android:id="@+id/navBarDistanceRtl"
                    style="@style/NavBarDistance"
                    android:textSize="19dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />
            </LinearLayout>
    </LinearLayout>

这是部分代码:

更改为 AlerterMode 然后返回到 non-alrter more 使得navBarDistanceLayoutRtl左对齐而不是右对齐

            if (bIsAlertMode)
            {

...
                if (rtlMode)
                {
                    width = view.findViewById(R.id.navBarDistanceLayoutRtl).getWidth();
                    android.widget.LinearLayout.LayoutParams DistanceBar = new LinearLayout.LayoutParams(width, android.widget.LinearLayout.LayoutParams.MATCH_PARENT);
                    DistanceBar.setMargins(3, 20, 3, 0);
                    view.findViewById(R.id.navBarDistanceLayoutRtl).setLayoutParams(DistanceBar);
                }
                else
                {
                    width = view.findViewById(R.id.navBarDistanceLayout).getWidth();
                    android.widget.LinearLayout.LayoutParams DistanceBar = new LinearLayout.LayoutParams(width, android.widget.LinearLayout.LayoutParams.MATCH_PARENT);
                    DistanceBar.setMargins(3, 20, 3, 0);
                    view.findViewById(R.id.navBarDistanceLayout).setLayoutParams(DistanceBar);

                }

...

            }
            else
            {
   ...

                if (rtlMode)
                {
                    width = view.findViewById(R.id.navBarDistanceLayoutRtl).getWidth();
                    android.widget.LinearLayout.LayoutParams DistanceBar = new LinearLayout.LayoutParams(width, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
                    DistanceBar.setMargins(20, 8, 3, 0);

                    DistanceBar.gravity = RelativeLayout.ALIGN_PARENT_RIGHT;

                    view.findViewById(R.id.navBarDistanceLayoutRtl).setLayoutParams(DistanceBar);

                    view.findViewById(R.id.navBarDistanceLayoutRtl).setVisibility(View.VISIBLE);
                    view.findViewById(R.id.navBarDistanceLayout).setVisibility(View.GONE);
                    distance = (TextView)view.findViewById(R.id.navBarDistanceRtl);
                    distanceUnit = (TextView)view.findViewById(R.id.navBarDistanceUnitRtl);
                }
                else
                {
                    width = view.findViewById(R.id.navBarDistanceLayout).getWidth();
                    android.widget.LinearLayout.LayoutParams DistanceBar = new LinearLayout.LayoutParams(width, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
                    DistanceBar.setMargins(0, 8, 3, 0);
                    view.findViewById(R.id.navBarDistanceLayout).setLayoutParams(DistanceBar);

                    DistanceBar.gravity = RelativeLayout.ALIGN_PARENT_LEFT;

                    view.findViewById(R.id.navBarDistanceLayoutRtl).setVisibility(View.GONE);
                    view.findViewById(R.id.navBarDistanceLayout).setVisibility(View.VISIBLE);
                    distance = (TextView)view.findViewById(R.id.navBarDistance);
                    distanceUnit = (TextView)view.findViewById(R.id.navBarDistanceUnit);

                }

                ...
            }
        }
4

1 回答 1

0

android:gravity只能用于 内部的内容LinearLayoutTextView或其他视图。如果你想要 RTL 或 LTR 布局,你必须使用android:layout_gravity.

于 2013-11-10T09:09:01.733 回答