gpt4 book ai didi

java - 如何以编程方式使 linearLayout 的 child 与 parent 正确对齐?

转载 作者:太空狗 更新时间:2023-10-29 12:44:51 25 4
gpt4 key购买 nike

我在另一个 linearLayout_parent 中有一个 linearLayout_child

我想让 linearLayout_child 与父级的右边对齐。

我该怎么做?

如果我希望所有 linearLayout_parent 的子项都与父项的右侧对齐怎么办?

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

更新

有什么工具可以让我在我的 Activity View 中查看当前的布局属性和属性吗?该代码更改了原始 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 的更改然后返回到 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);

}

...
}
}

最佳答案

尝试像这样设置gravity..

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

layoutParams.gravity = RelativeLayout.ALIGN_PARENT_RIGHT;

linearlayout.setLayoutParams(layoutParams);

关于java - 如何以编程方式使 linearLayout 的 child 与 parent 正确对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19840489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com