在ConstraintLayout内为视图设置权​​重 [英] Set weight for a View inside ConstraintLayout

查看:82
本文介绍了在ConstraintLayout内为视图设置权​​重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

以下布局包括flagItem作为LinearLayout,在其中以编程方式向其中添加标志imageViews:

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>

问题是: ImageViews可能与line_number视图冲突.有什么解决方法可以在imageView的末尾显示three dots,然后与line_number冲突,就像我们在TextView中使用

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:

我尝试过:

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

但这不是解决方案,因为它将phone_number移到了不需要的右角.

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

推荐答案

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

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

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

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

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

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

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);
     }
}

这篇关于在ConstraintLayout内为视图设置权​​重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆