【问题标题】:Set weight for a View inside ConstraintLayout在 ConstraintLayout 中为 View 设置权重
【发布时间】:2018-12-16 03:57:11
【问题描述】:

我在 Github 中有以下项目:https://github.com/Ali-Rezaei/Contacts,我在其中根据联系人的号码显示国家/地区的标志。

以下布局包括flagItem 作为一个LinearLayout,我在其中以编程方式将标志imageViews 添加到它:

<android.support.constraint.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:id="@+id/detail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/contact_selector"
    android:minHeight="64dp">

    <TextView
        android:id="@+id/image_text"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:background="@drawable/contact_circle"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="@+id/guideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        tools:text="A.R" />

    <TextView
        android:id="@+id/contact_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:paddingStart="16dp"
        android:paddingLeft="16dp"
        android:textSize="17sp"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toEndOf="@+id/image_text"
        tools:layout_constraintEnd_toStartOf="@id/phone_type"
        tools:text="Ali Rezaei" />

    <LinearLayout
        android:id="@+id/flagItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingStart="16dp"
        android:paddingLeft="16dp"
        app:layout_constraintStart_toStartOf="@+id/contact_name"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <TextView
        android:id="@+id/phone_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="13sp"
        app:layout_constraintStart_toEndOf="@id/flagItem"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        tools:text="+989121895634" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <TextView
        android:id="@+id/phone_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-light"
        android:paddingEnd="8dp"
        android:paddingRight="8dp"
        android:textSize="13sp"
        app:layout_constraintBottom_toBottomOf="@+id/guideline"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        tools:text="Mobile" />

    <TextView
        android:id="@+id/line_number"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:background="@drawable/contact_circle"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="@id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/guideline"
        tools:text="2" />

    <View
        android:id="@+id/line"
        style="@style/LineStyle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/image_text" />

</android.support.constraint.ConstraintLayout>

问题是: ImageViews 可能与line_number 视图冲突。在与line_number 冲突之前,是否有任何解决方案可以在 imageViews 的末尾显示 three dots,正如我们在 TextView 中使用的那样:

android:ellipsize="end"
android:maxLines="1"

Addenda

我试过了:

<LinearLayout
    android:id="@+id/flagItem"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toStartOf="@id/line_number" />

但这不是一个解决方案,因为它会将phone_number 移动到不需要的右上角。

【问题讨论】:

    标签: android android-linearlayout android-constraintlayout


    【解决方案1】:

    将标志项设置为匹配父项,将数字设置为匹配父项,然后给标志的权重为 0.9,数字的权重为 0.1,然后围绕它的线性布局总和为 1。

    这将在线性布局中均匀分布视图。然后,您可以根据您希望它们占用多少空间来更改权重。

    【讨论】:

    • 它们在一个 ConstraintLayout 中。可以在里面设置weight吗?
    • 不,你只能在线性布局内设置权重,我建议学习如何在使用约束布局时使用它们,应用程序的视觉效果会在不同尺寸的显示器上发生变化。使用线性布局和权重可以让您的 UI 在大多数手机上均匀分布。
    • 如果你想使用约束布局,我建议将标志的末尾约束到数字的开头并设置宽度以匹配约束。
    • app:layout_constraintEnd_toStartOf="@id/line_number" 不是解决方案。我已经在问题的Addenda 部分写过。
    • @Ali 电话号码呢?那应该总是可见的吗?还可以尝试在Addenda 上使用wrap_content 作为宽度,它不会将数字推到最后。在这种情况下,您只需设置 app:layout_constraintHorizontal_bias="0"
    【解决方案2】:

    我最终在布局中添加了一个 imageView:

    <ImageView
            android:id="@+id/flagImageView"
            android:layout_width="@dimen/dimen_flag_image_view_width"
            android:layout_height="@dimen/dimen_flag_image_view_height"
            android:layout_marginStart="16dp"
            app:layout_constraintStart_toStartOf="@+id/contact_name"
            app:layout_constraintTop_toTopOf="@+id/guideline"
            tools:ignore="ContentDescription"
            tools:src="@drawable/flag_sweden" />
    
        <LinearLayout
            android:id="@+id/flagItem"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingStart="16dp"
            android:paddingEnd="@dimen/dimen_flag_image_view_margin_end"
            app:layout_constraintEnd_toStartOf="@id/line_number"
            app:layout_constraintStart_toStartOf="@+id/contact_name"
            app:layout_constraintTop_toTopOf="@+id/guideline"
            tools:ignore="RtlSymmetry" />
    

    当我们只有一个数字时,在 ImageView 中显示标志:

    if (numbers.size() == 1) {
       flagImageView.setImageResource(getFlagResID(context, countryCodeNumber.regionCode));
    }
    

    否则将运行时 ImageViews 添加到 LinearLayout:

    List<ImageView> list = new ArrayList<>();
    
    for (int childPosition = 0; childPosition < numbers.size(); childPosition++) {
    
         ContactPhoneNumber phoneNumber = numbers.get(childPosition);
    
         ImageView imageView = getFlagImageView(context, phoneNumber.regionCode);
         if (!list.contains(imageView)) {
             flagItem.addView(imageView);
             list.add(imageView);
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多