在此constraintlayout中,我无法使TextView左对齐。

我有一个奇怪的事情,就是无论我做什么TextView都会跳到右边。

这是一张照片:
android - 迁移到androidX后,constraintlayout View 会跳到右侧-LMLPHP

我刚刚迁移到androidX,也许与这个dunno无关。

这是xml布局建议

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pref_category_remote_battery_title"
        android:textAppearance="?android:attr/textAppearanceListItem"
        android:textColor="@color/default_400"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:text="@string/pref_category_remote_battery_switch_summ"

        android:textAppearance="?android:attr/textAppearanceSmall"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="30dp" />

    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switch_compat"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="8dp"
        android:checked="false"
        android:textOff="OFF"
        android:textOn="ON"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline3"
        app:layout_constraintTop_toTopOf="parent"
        app:showText="true" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.74722224" />

</androidx.constraintlayout.widget.ConstraintLayout>


这里的build.gradle依赖项:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'androidx.preference:preference:1.0.0'
    implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core:1.0.2'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.0.0'

    implementation 'com.google.android.gms:play-services-auth:16.0.1'
    implementation 'com.google.firebase:firebase-core:16.0.9'
    implementation 'com.google.firebase:firebase-auth:17.0.0'
    implementation 'com.google.firebase:firebase-firestore:19.0.0'

    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
    implementation 'com.firebaseui:firebase-ui-auth:5.0.0'

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'org.greenrobot:eventbus:3.1.1' } apply plugin: 'com.google.gms.google-services' apply plugin: 'kotlin-android-extensions' repositories {
    mavenCentral() }

最佳答案

如果希望它保留在左侧,可以将android:layoutDirection="ltr"添加到布局中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layoutDirection="ltr">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="pref_category_remote_battery_title"
    android:textAppearance="?android:attr/textAppearanceListItem"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView2" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="8dp"
    android:text="pref_category_remote_battery_switch_summ"
    android:textAppearance="?android:attr/textAppearanceSmall"
    app:layout_constraintEnd_toStartOf="@+id/guideline3"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/switch_compat"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="24dp"
    android:layout_marginBottom="8dp"
    android:checked="false"
    android:textOff="OFF"
    android:textOn="ON"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline3"
    app:layout_constraintTop_toTopOf="parent"
    app:showText="true" />

 <android.support.constraint.Guideline
    android:id="@+id/guideline3"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.74722224" />

</android.support.constraint.ConstraintLayout>


因此它将保持在左侧,如下所示:

android - 迁移到androidX后,constraintlayout View 会跳到右侧-LMLPHP

10-07 12:44