2017-08-31 101 views
0

我需要一些关于Android布局的帮助。 我有以下代码:Android - ConstraintLayout - 椭圆大写文字结尾

<?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" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

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

<android.support.constraint.Guideline 
    android:id="@+id/guideline2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    app:layout_constraintGuide_percent="0.725" /> 

<TextView 
    android:id="@+id/data_field_1_name" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="10dip" 
    android:layout_marginTop="10dip" 
    app:layout_constraintLeft_toLeftOf="@id/guideline" 
    app:layout_constraintRight_toLeftOf="@id/guideline2" 
    app:layout_constraintTop_toTopOf="parent" 
    tools:text="ACTIVE" /> 

<TextView 
    android:id="@+id/data_field_1_value" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="15dip" 
    android:layout_toEndOf="@id/data_field_1_name" 
    app:layout_constraintBaseline_toBaselineOf="@id/data_field_1_name" 
    app:layout_constraintLeft_toLeftOf="@id/guideline2" 
    app:layout_constraintRight_toRightOf="parent" 
    tools:text="1750" /> 

<TextView 
    android:id="@+id/data_field_2_name" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="10dip" 
    android:layout_marginTop="8dip" 
    app:layout_constraintLeft_toLeftOf="@id/guideline" 
    app:layout_constraintRight_toLeftOf="@id/guideline2" 
    app:layout_constraintTop_toBottomOf="@id/data_field_1_name" 
    tools:text="ACTIVE" /> 

<TextView 
    android:id="@+id/data_field_2_value" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="15dip" 
    android:layout_toEndOf="@id/data_field_2_name" 
    app:layout_constraintBaseline_toBaselineOf="@id/data_field_2_name" 
    app:layout_constraintLeft_toLeftOf="@id/guideline2" 
    app:layout_constraintRight_toRightOf="parent" 
    tools:text="1750" /> 


<TextView 
    android:id="@+id/data_field_3_name" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="10dip" 
    android:layout_marginTop="8dip" 
    app:layout_constraintLeft_toLeftOf="@id/guideline" 
    app:layout_constraintRight_toLeftOf="@id/guideline2" 
    app:layout_constraintTop_toBottomOf="@id/data_field_2_name" 
    tools:text="ACTIVE" /> 

<TextView 
    android:id="@+id/data_field_3_value" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="15dip" 
    android:layout_toEndOf="@id/data_field_3_name" 
    app:layout_constraintBaseline_toBaselineOf="@id/data_field_3_name" 
    app:layout_constraintLeft_toLeftOf="@id/guideline2" 
    app:layout_constraintRight_toRightOf="parent" 
    tools:text="1750" /> 

<TextView 
    android:id="@+id/value" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="15dip" 
    android:layout_marginStart="15dip" 
    android:textSize="25sp" 
    app:layout_constraintBaseline_toBaselineOf="@id/data_field_2_name" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintLeft_toLeftOf="parent" 
    tools:text="17.40" /> 

<ImageView 
    android:id="@+id/flag" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="0dp" 
    android:layout_marginTop="2dp" 
    android:src="@drawable/ic_launcher_background" 
    app:layout_constraintEnd_toEndOf="@+id/unit" 
    app:layout_constraintStart_toStartOf="@+id/unit" 
    app:layout_constraintTop_toTopOf="parent" /> 

<android.support.v7.widget.AppCompatTextView 
    android:id="@+id/unit" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginStart="2dp" 
    android:gravity="center_horizontal" 
    app:layout_constraintBaseline_toBaselineOf="@id/value" 
    app:layout_constraintStart_toEndOf="@+id/value" 
    tools:text="KG" /> 

<TextView 
    android:id="@+id/label" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginStart="15dip" 
    app:layout_constraintBaseline_toBaselineOf="@id/data_field_3_name" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toLeftOf="@id/guideline" 
    tools:text="TOTAL" /> 
</android.support.constraint.ConstraintLayout> 

输出是:enter image description here

我的问题是:如果主值(17.40)过大,该值应该是“ellipsized”结束,但它通过“活动”文本。

对大量文本的输入是:enter image description here

我需要这样的东西,对于大价值: enter image description here

PS:单位和和国旗应该在主值的末尾总是显示。

你能帮我解答一下吗?我尝试了很多想法,但没有找到解决方案。

回答

1

对于

1)您必须指定为固定长度,宽度的TextView将0dp无法正常工作。 2)你需要将ellipsize属性设置为“结束”到textview。

3)的maxlines一起属性设置为1。

测试这和正常工作。

enter image description here

你可以保持固定宽度,只要你喜欢..

<TextView 
    android:id="@+id/value" 
    android:layout_width="140dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="15dip" 
    android:ellipsize="end" 
    android:maxLines="1" 
    android:layout_marginStart="15dip" 
    android:textSize="25sp" 
    app:layout_constraintBaseline_toBaselineOf="@id/data_field_2_name" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintLeft_toLeftOf="parent" 
    tools:text="17.40000000000000" /> 
+0

2和3对,我忘了补充他们的例子中,他们已经在样式。 但是对于1,我不能使用固定值。我需要它wrap_content –

+0

所以wrap_content的属性是它会扩大尽可能多的,这是我的代码1行,所以它会一直走到屏幕宽度的结尾,然后你会在最后得到点。但要与你想要的,你必须使用固定的大小,否则不会工作 –

0

假设你有你的valueTextView成立ellipsize本身,你可以通过编程设置TextView的最大解决这个ConstraintLayout完成了所有视图布局后的宽度。

我创建了一个小应用程序,它只是膨胀了您在问题中发布的布局(尽管我必须将该标志更改为我的图像)。我说这些ATTRS到value观点:

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

这是我的Java代码:

public class MainActivity extends AppCompatActivity { 

    private TextView value; 
    private TextView data2; 
    private TextView unit; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     value = (TextView) findViewById(R.id.value); 
     data2 = (TextView) findViewById(R.id.data_field_2_name); 
     unit = (TextView) findViewById(R.id.unit); 

     value.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
        value.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
       } 
       else { 
        value.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
       } 

       setMaxWidth(); 
      } 
     }); 
    } 

    private void setMaxWidth() { 
     ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) value.getLayoutParams(); 

     int maxWidth = data2.getLeft() - value.getLeft(); 
     int maxWidthConsideringUnits = maxWidth - unit.getWidth(); 
     int maxWidthIncludingMargin = maxWidthConsideringUnits - params.getMarginEnd(); 

     value.setMaxWidth(maxWidthIncludingMargin); 
    } 
} 

下面是一些截图:

enter image description here enter image description here

0
  1. 设置布局宽度到0dp

机器人:layout_width = “0dp”

  • Set Right_toRightOfLeft_toRightOf
  • 应用程式:layout_constraintLeft_toRightOf =“@ + id/listingImageView“ 应用:layout_constraintRight_toRightOf = “父” 应用:layout_constraintTop_toTopOf = “父”