Bre*_*ell 5 android android-layout
我正在尝试在包含两个 TextView 和一个 ImageButton 的复杂布局中处理 RTL(从右到左)布局。
然而,布局方向永远不会返回 RTL 作为布局方向,直到 onResume() 方法被调用一段时间后。调用ViewCompat.getLayoutDirection(getView())总是在我检查过的生命周期中的每个点返回 LTR,除了 inonStop()
我们一直使用这个方法来处理 RecyclerView 中的绑定视图,但这是我们第一次尝试在 RecyclerView 之外的复杂布局上使用它。
有没有其他人看到这种行为或知道如何(或何时)获得正确的布局方向?
这是我为处理 RTL 所做的工作:
我的片段.java:
private TextView title;
private TextView subtitle;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_preview_audio, container, false);
loadAsset();
validateAsset();
int layoutDirection = ViewCompat.getLayoutDirection(container);
setupUi(layoutDirection);
populateData();
return view;
}
private void setupUi(int layoutDirection) {
int gravity = GravityCompat.getAbsoluteGravity(GravityCompat.START, layoutDirection);
title.setGravity(gravity);
subtitle.setGravity(gravity);
}
Run Code Online (Sandbox Code Playgroud)
details.xml(包含在 fragment_preview_audio 中)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/details_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="@color/text_dark_grey"
android:textStyle="bold"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="1"
/>
<ImageButton
android:id="@+id/menu_button"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/ic_more_vert_black_24dp"
style="?android:attr/buttonBarButtonStyle"
tools:ignore="ContentDescription"/>
</LinearLayout>
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/text_light_grey"
android:singleLine="false"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
/>
Run Code Online (Sandbox Code Playgroud)
使用具有需要预测开始或结束的布局重力的子组件的容器小部件,并将其指示为方法内的参数ViewCompat.getLayoutDirection(container)。我在 itemdecorator 中使用了 recyclerview,它终于起作用了。
| 归档时间: |
|
| 查看次数: |
931 次 |
| 最近记录: |