2017-08-31 106 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 = 「父」