Android ViewCompat.getLayoutDirection不';在onResume()之后,才能返回正确的布局

Android ViewCompat.getLayoutDirection不';在onResume()之后,才能返回正确的布局,android,android-layout,Android,Android Layout,我试图在包含两个TextView和一个ImageButton的复杂布局中处理RTL(从右到左)布局 但是,直到调用onResume()方法后的某个时间,布局方向才返回RTL作为布局方向。调用ViewCompat.getLayoutDirection(getView())总是在我检查的生命周期中的每个点返回LTR,除了在onStop() 我们一直使用此方法来处理RecyclerView中的绑定视图持有者,但这是我们第一次尝试在RecyclerView之外的复杂布局上使用它 有没有其他人见过这种行为

我试图在包含两个TextView和一个ImageButton的复杂布局中处理RTL(从右到左)布局

但是,直到调用onResume()方法后的某个时间,布局方向才返回RTL作为布局方向。调用
ViewCompat.getLayoutDirection(getView())
总是在我检查的生命周期中的每个点返回LTR,除了在
onStop()

我们一直使用此方法来处理RecyclerView中的绑定视图持有者,但这是我们第一次尝试在RecyclerView之外的复杂布局上使用它

有没有其他人见过这种行为,或者知道如何(或何时)获得正确的布局方向

以下是我为处理RTL所做的工作:

MyFragment.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);
}
details.xml(包含在片段\u预览\u音频中)



使用容器小部件,该小部件具有需要预测开始或结束布局重力的子组件,并将其指示为方法
ViewCompat.getLayoutDirection(容器)
中的参数。我在我的itemdecorator中使用了recyclerview,它终于起作用了

<?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"
    />