【问题标题】:ConstraintLayout children not properly aligned in parent viewConstraintLayout 子级在父视图中未正确对齐
【发布时间】:2017-07-25 00:55:50
【问题描述】:

我试图在父视图中有两个文本框,一个在左侧带有静态文本,另一个包含信息。我希望信息文本在水平方向占用尽可能多的空间,但不与标题重叠,这应该可以通过 ConstraintLayout 实现。标题显示正确,但我无法将信息文本框对齐到屏幕右侧,不确定是什么问题。我正在使用我认为是最新版本的 ConstraintLayout 1.0.2。

这是 ConstraintLayout 的 xml 文件。

<?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:id="@+id/information"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="40dp"
    android:paddingStart="20dp"
    android:paddingEnd="20dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/error_layout"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/summary"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center|end"
        android:text="@string/error_layout"
        android:textSize="16sp"
        android:ellipsize="end"
        android:maxLines="1"
        android:background="@color/accent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

这是我用来创建新视图的类。

public class InformationView extends RelativeLayout {
    public InformationView(Context context, String title, String summary, int color) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.view_information, null);
        addView(view);

        setBackgroundColor(color);

        TextView titleView = (TextView) view.findViewById(R.id.title);
        TextView summaryView = (TextView) view.findViewById(R.id.summary);

        titleView.setText(title);
        summaryView.setText(summary);
    }
}

这里是我添加视图的地方。

    LinearLayout information = (LinearLayout) findViewById(R.id.information);
    InformationView packageView = new InformationView(context, getString(R.string.package_layout), appInfo.getAPK(), getResources().getColor(R.color.grey));
    InformationView versionView = new InformationView(context, getString(R.string.version_layout), appInfo.getVersion(), getResources().getColor(R.color.grey_dark));
    InformationView appSizeView = new InformationView(context, getString(R.string.size_layout), getString(R.string.development_layout), getResources().getColor(R.color.grey));
    InformationView cacheSizeView = new InformationView(context, getString(R.string.cache_size_layout), getString(R.string.development_layout), getResources().getColor(R.color.grey_dark));
    InformationView dataFolderView = new InformationView(context, getString(R.string.data_layout), appInfo.getData(), getResources().getColor(R.color.grey));
    InformationView sourceFolderView = new InformationView(context, getString(R.string.source_layout), appInfo.getSource(), getResources().getColor(R.color.grey_dark));
    informations.addView(packageView);
    informations.addView(versionView);
    informations.addView(appSizeView);
    informations.addView(cacheSizeView);
    informations.addView(dataFolderView);
    informations.addView(sourceFolderView);

这就是视图的样子。第二个文本框似乎与第一个文本框对齐,尽管它应该更靠右。

【问题讨论】:

    标签: java android


    【解决方案1】:

    在您的summary 视图中,将宽度更改为0dp 并添加constraintLeft。你会得到这个:

    <TextView
        android:id="@+id/summary"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:gravity="center|end"
        android:text="@string/error_layout"
        android:textSize="16sp"
        android:ellipsize="end"
        android:maxLines="1"
        android:background="@color/accent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/title"
        app:layout_constraintTop_toTopOf="parent" />
    

    【讨论】:

    • 刚刚试过,现在信息完全是gone。不过,它在 android studio 的设计视图中看起来是正确的。
    猜你喜欢
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    相关资源
    最近更新 更多