android - constraintlayout View 迁移到 androidX 后向右跳转

标签 android

在这个 constraintlayout 中,我无法让 TextView 左对齐。

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

这是一张图片: enter image description here

我刚迁移到 androidX 可能与这个不知道有关..

这是 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>

所以它会留在左边,看起来像这样:

enter image description here

关于android - constraintlayout View 迁移到 androidX 后向右跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57639477/

相关文章:

android - 蓝牙 startDiscovery() 不适用于 Android 10

c# - Mono for Android Widget 教程

android - 带有 Android 东西的 Raspberry Pi 3 上的屏幕方向

android - 如何在 Android 中以 XML 格式发送 SOAP 请求和解析 SOAP 响应?

android - 开始 Activity 后立即调用 OnPause 和 OnStop()

android - 在设置动态图像选择器上卡住应用程序

java - Android Studio Java 位图工厂返回 null

Android 正则表达式 HTML

java - 改造和 SimpleXmlConverterFactory

java - 从 Java 调用 javascript 函数