Set weight for a View inside ConstraintLayout

那年仲夏 提交于 2019-12-24 21:05:02

问题


I have the following project in Github : https://github.com/Ali-Rezaei/Contacts, where I show flags of countries based on contact's numbers.

The following layout includes flagItem as a LinearLayout in which I add the flag imageViews to it programmatically :

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

Problem is : ImageViews may conflict with line_number view. Is there any solution to show three dotsat the end of imageViews before conflicting with line_number as we show in TextView using :

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

Addenda :

I tried :

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

But it is not a solution because it moves phone_number to the right corner which is not desired.


回答1:


Set the flag item to match parent, and the number to match parent, then give the flag's a weight of 0.9, the number a weight of 0.1 then the linear layout around it a sum of 1.

This will evenly distribute the views in the linear layout. You can then change the weights depending on how much space you want them to take.




回答2:


I ended up adding an imageView to the layout :

<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" />

And display flag in ImageView when we have only one number :

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

Otherwise add runtime ImageViews to 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);
     }
}


来源:https://stackoverflow.com/questions/53783813/set-weight-for-a-view-inside-constraintlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!