如何使linearLayout的子项以编程方式与父项对齐?

时间:2013-11-07 15:54:59

标签: java android android-layout layout

我在另一个linearLayout_child内有一个linearLayout_parent

我想让linearLayout_child与父母的权利保持一致。

我该怎么做?

如果我希望所有linearLayout_parent的孩子都与父母的孩子对齐,该怎么办?

我想以编程方式完成它们。

更新

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

这是我的xml:

            

        <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的更改然后返回到非更多会使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);

                }

                ...
            }
        }

3 个答案:

答案 0 :(得分:2)

尝试像这样设置gravity ..

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);

    layoutParams.gravity = RelativeLayout.ALIGN_PARENT_RIGHT;

    linearlayout.setLayoutParams(layoutParams);

答案 1 :(得分:1)

您可以使用:

  
      
  • 布局参数 GRAVITY Doc
  •   
  • spacer views(设置 layout_width =“0dp” layout_weight =“1”的简单视图
  •   

答案 2 :(得分:0)

我在我的案例中找到了一个解决方案,我在Linear Layout添加了视图,只需将gravity设置为TEXT_ALIGNMENT_VIEW_START:

    LinearLayout row = new LinearLayout(Control.this);
    row.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    row.setGravity(View.TEXT_ALIGNMENT_VIEW_START);