2016-03-04 27 views
0

我使用輸入字段的解決方案,字段說明採用左側空格,值使用右側空格。它通常有效,但如果最正確的文本太寬,則不會。然後,它只是奠定了最左邊的文本後不是得到它要求的"wrap_content"的:兩個TextViews共享左右行,當右側文本變得過大時失敗

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:layout_marginTop="20dp" 
    android:background="@drawable/edittext_background" 
    android:orientation="horizontal" 
    android:padding="10dp"> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:text="Make of car" 
     android:textColor="@android:color/black" 
     android:textSize="19dp"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:gravity="center_vertical|end" 
     android:singleLine="true" 
     android:text="Ford Focus 2.0 Gold edition with ..." 
     android:ellipsize="end" 
     android:textColor="@android:color/black" 
     android:textSize="@dimen/edittext_value" /> 
</LinearLayout> 

edittext_background

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="@color/input_field_grey" /> 
    <corners android:radius="6dp" /> 
</shape> 

結果是這樣的:

enter image description here

它的工作原理如下,當值文本佔用的空間大於空間時,只需像這樣省略類型文本:

enter image description here

但是,如果該值文本太寬,類型和值文本省略號被忽略,整個文本只是附加到這樣的權利:

enter image description here

我的目標值文本佔據了所有空間並使用省略號來限制文本,如下所示:

enter image description here

有人有更好的解決方案嗎?

回答

2

嘗試在TextView中移除重量並設置其寬度爲WRAP_CONTENT

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginLeft="20dp" 
android:layout_marginRight="20dp" 
android:layout_marginTop="20dp" 
android:background="#60000000" 
android:orientation="horizontal" 
android:padding="10dp"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@null" 
    android:ellipsize="end" 
    android:singleLine="true" 
    android:text="Make of car" 
    android:textColor="@android:color/black" 
    android:textSize="19dp"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@null" 
    android:ellipsize="end" 
    android:gravity="center_vertical|end" 
    android:maxLines="1" 
    android:text="Ford Focus 2.0 Gold edition with huge amount getting sort out" 
    android:textColor="@android:color/black" 
    android:textSize="17sp"/> 
</LinearLayout> 

如果這不是你想要的,請嘗試使用RelativeLayout的與alignParentLeft和alignParentRi ght分別爲左側和右側文字瀏覽。 android:layout_toLeftOf屬性可防止文本重疊:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginLeft="20dp" 
android:layout_marginRight="20dp" 
android:layout_marginTop="20dp" 
android:background="#60000000" 
android:padding="10dp"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_toLeftOf="@+id/right_value" 
    android:background="@null" 
    android:ellipsize="end" 
    android:maxLines="1" 
    android:text="Make of car" 
    android:textColor="@android:color/black" 
    android:textSize="19sp"/> 

<TextView 
    android:id="@+id/right_value" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:ellipsize="end" 
    android:gravity="center_vertical|end" 
    android:maxLines="1" 
    android:text="Ford Focus 2.0 Gold edition with huge amount getting sort out" 
    android:textColor="@android:color/black" 
    android:textSize="17sp"/> 
</RelativeLayout> 
+0

這兩個建議很好地工作,RelativeLayout完全符合我的想法。謝謝! – Aksel

0

您正在使用LinearLayout,所以只需使用加權值:)將福特福克斯2.0黃金版的重量與1相加,確保無論屏幕尺寸如何,其他屏幕尺寸都將顯示爲全屏,而其他TextView乾脆把剩餘的空間:)

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:layout_marginTop="20dp" 
    android:background="@drawable/edittext_background" 
    android:orientation="horizontal" 
    android:padding="10dp"> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:ellipsize="end" 
     android:singleLine="true" 
     android:text="Make of car" 
     android:textColor="@android:color/black" 
     android:textSize="19dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:gravity="center_vertical|end" 
     android:singleLine="true" 
     android:text="Ford Focus 2.0 Gold edition with ..." 
     android:ellipsize="end" 
     android:textColor="@android:color/black" 
     android:textSize="@dimen/edittext_value" 
     android:layout_weight ="1"/> 
</LinearLayout> 
+0

謝謝。它有副作用,儘管在實際用完空間之前左邊的文字會被縮短。如果在正確的文本上也加上權重= 1,那麼左邊的文本只能佔據50%。我希望左邊的文本填充任何可用的內容,但是當沒有足夠的空間時,正確的文本優先,「勝利」。 – Aksel

+0

你可以請做2改變,並檢查:)在汽車的Textview設置寬度wrap_content並將LinearLayout weight_sum設置爲1 :) –

+0

謝謝,但左側的文本仍然優先。感謝您的努力:) – Aksel

相關問題