小编pap*_*e96的帖子

循环约束和“缺少约束”错误

我正在玩弄 提供的圆形定位ConstraintLayout。在下面的示例应用程序中,我有一个TextView在屏幕上居中和一个半径为50dp s、角度为45 度ImageViews 的圆形约束。它应该受到完全约束,因为不再有以 开头的 XML 属性,所以我假设我已经完全声明了它的预期位置。但是,Android Studio 向我发出以下警告:TextViewapp:layout_constraintCircleImageView

这种观点不受约束。它只有设计时位置,因此除非添加约束,否则它将在运行时跳转到 (0,0)

运行应用程序证明我是对的,因为受约束的视图位于正确的位置并且不会跳转到布局的开头。

所以我的问题是:我是否缺少任何额外的约束,或者 Android Studio 是否因某种原因无法识别循环约束有效?

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="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/hello_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher_background"
        app:layout_constraintCircle="@id/hello_text"
        app:layout_constraintCircleAngle="45"
        app:layout_constraintCircleRadius="50dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

android android-layout android-studio android-constraintlayout

6
推荐指数
1
解决办法
1226
查看次数

将视图与旋转的 TextView 的顶部对齐

我有一个TextView包含一些显示的文本,android:rotation="270"因此它从下到上读取。
我在布局中有另一个视图(ButtonImageView或其他一些简单View对象)。

我想将第二个视图与旋转文本的顶部对齐,如下所示:

我想要实现的布局

我正在使用,ConstraintLayout所以我尝试了显而易见的方法,分配constraintBottom_toTopOf="@id/text"给视图。但是,它不会将 与所需位置对齐,而是与未旋转位置View的顶部对齐。这可能是因为(正如您在图像上看到的)边界框没有旋转,只有显示的文本旋转。TextViewTextView

使用约束对齐时得到的布局

有没有办法让与 的实际顶部View对齐,在其最后一个字符的顶部,而无需硬编码值?或者我必须通过代码来解决它吗?TextViewTranslation

这是我重现问题的简单布局:

<?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="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:rotation="270"/>

    <Button
        android:layout_width="200dp"
        android:layout_height="64dp"
        android:text="Align me!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@id/text"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

android android-layout android-constraintlayout

5
推荐指数
1
解决办法
1016
查看次数

DataBindingUtil 将布局膨胀为 null

我正在使用 Android Jetpack 导航组件开发单个活动应用程序。在其中一个片段上,我使用了内置的数据绑定工具。奇怪的是,尽管它在前一周还可以工作,但今天却无缘无故地彻底坏了。

\n\n

设置:
\n我与绑定一起使用的片段具有以下布局文件:

\n\n
<?xml version="1.0" encoding="utf-8"?>\n<layout \n    xmlns:android="http://schemas.android.com/apk/res/android"\n    xmlns:app="http://schemas.android.com/apk/res-auto"\n    xmlns:tools="http://schemas.android.com/tools">\n\n    <data>\n    </data>\n\n    <ScrollView\n        android:layout_width="match_parent"\n        android:layout_height="match_parent"\n        android:id="@+id/example_layout_root"\n        tools:context=".example.ExampleFragment"\n        android:background="@color/main_1"\n        >\n\n        ...\n\n    </ScrollView>\n</layout>\n
Run Code Online (Sandbox Code Playgroud)\n\n

我已经删除了主要内容,但它显示我有一个<layout>元素作为根,同时定义了数据和片段布局部分。

\n\n

片段代码如下:

\n\n
class ExamleFragment : Fragment() {\n\n    private val viewModel: ExampleViewModel by sharedViewModel()\n\n    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {\n        // Inflate the layout for this fragment\n        val binding : FragmentExampleBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_example, container, false)\n        // This one also does not work\n        // val binding …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-fragments android-databinding android-viewmodel

5
推荐指数
2
解决办法
6023
查看次数