2014-09-06 29 views
2

這是我第一次張貼在這個論壇上,希望它會沒事的:)2 TextViews,留下了省略號,右接NOWRAP,單線

我深化發展的Android應用程序在我的城市公共交通。

Here is what I have

[ |short destination ||next departure| ] 
[ |way too long dest...||next departure| ] 

Here is what I want:

[ |short destination||next departure| ] 
[ |way too long dest...||next departure| ] 

下面是一個更完整的例子:s28.postimg.org/5gejnvfd9/actual2.png

奇怪顏色的背景只是在這裏輕鬆地識別佈局/文字瀏覽。你也可以忽略棕色線(這是可以的)。

基本上,我想擁有長度可變的目的地[紅色背景],在其右側,我想要第一次出發時間[綠色背景]。一切都在一條線上。

我需要始終將首次出發信息完全顯示(nowrap)。目的地可以用省略號(...)包裹。 [可選問題,如何用'。'替換省略號'...' ?]

這裏是最好的工作代碼,我到目前爲止有:

<RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/txtTitleDestination" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_toLeftOf="@+id/txtTitleFirstDeparture" 
      android:background="#FF0000" 
      android:ellipsize="end" 
      android:maxLines="1" 
      android:padding="0dp" 
      android:scrollHorizontally="true" 
      android:textSize="18sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/txtTitleFirstDeparture" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="0dp" 
      android:layout_marginRight="0dp" 
      android:layout_alignParentRight="true" 
      android:background="#00FF00" 
      android:ellipsize="none" 
      android:maxLines="1" 
      android:padding="0dp" 
      android:textSize="18sp" 
      android:textStyle="bold" 
      /> 

    </RelativeLayout> 

我試過TableLayout和LinearLayour代替RelativeLayout的,但沒有成功:(

任何想法如何我能做到這一點?

提前感謝!

Louloox

[解決] 只要有輕輕修改valbertos答案:

titleDestination.post(new Runnable() { 
     @Override 
     public void run() {    
      int widthTextViewDeparture = measureTextWidthTextView(titleFirstTime, pContext); 
      int widthTextViewDestination = titleDestination.getWidth(); 
      int widthTextViewParent = rl_parent.getWidth(); 
      if(widthTextViewDestination + widthTextViewDeparture > widthTextViewParent) { 
       titleDestination.setWidth(widthTextViewParent - widthTextViewDeparture); 
       titleDestination.setEllipsize(TruncateAt.END); 
       titleDestination.setHorizontallyScrolling(true); 
      } 
     } 
    }); 

設置的省略號僅在必要時使正常截斷文本。

Before: 
Lingolsheim Thiergaten --> Lingolsheim...  [1'23"] 21h23 

With the modification: 
Lingolsheim Thiergaten --> Lingolsheim Thi... [1'23"] 21h23 

再次感謝:)

+0

下面是一個更完整的例子:http://s28.postimg.org/5gejnvfd9/actual2.png – louloox 2014-09-06 17:58:10

+0

你必須understant那是什麼,內容是動態地從一個WebService檢索。因此,我不知道目的地名稱有多大,所以我肯定需要「目標名稱」TextView動態(根據寬度),我不希望這個「隱藏」右邊TextView中。 – louloox 2014-09-06 18:00:24

回答

1

要做你要求的,你必須根據第二個視圖的文本寬度動態調整第一個視圖的寬度。

//代碼

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

    final TextView tv_1 = (TextView) findViewById(R.id.tv_1); 
    tv_1.post(new Runnable() { 
     @Override 
     public void run() { 
      View rl_parent = findViewById(R.id.rl_parent); 
      TextView tv_2 = (TextView) findViewById(R.id.tv_2); 
      int widthTextView2 = measureTextWidthTextView(tv_2); 
      if(tv_1.getWidth() + widthTextView2 > rl_parent.getWidth()) { 
       tv_1.setWidth(tv_1.getWidth() - widthTextView2); 
      } 
     } 
    }); 
} 

private int measureTextWidthTextView(TextView textView) { 
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(getScreenWidth(), View.MeasureSpec.AT_MOST); 
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 
    textView.measure(widthMeasureSpec, heightMeasureSpec); 
    return textView.getMeasuredWidth(); 
} 

private int getScreenWidth() { 
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 
    Display display = wm.getDefaultDisplay(); 
    Point size = new Point(); 
    display.getSize(size); 
    return size.x; 
} 

//佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="300dp" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:id="@+id/rl_parent" 
     android:layout_width="match_parent" 
     android:layout_height="40dp" 
     android:background="#348D63"> 

     <TextView 
      android:id="@+id/tv_1" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:background="#BD160B" 
      android:gravity="center_vertical" 
      android:maxLines="1" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:text="Elsau Elsau Elsau Elsau Elsau Elsau Elsau" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/tv_2" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_toRightOf="@+id/tv_1" 
      android:gravity="center_vertical" 
      android:minWidth="50dp" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:text="[11:30 14:23]" 
      android:textStyle="bold" /> 

    </RelativeLayout> 

    <!-- Rest of the layout --> 

</LinearLayout> 

使用Android:MAXLINES = 「1」,而不是機器人:單線= 「true」 以擺脫掉醜點-as我在我的例子中做了。

此外,我建議您使用include作爲「時間」部分,而不是重複兩次TextViews。我只是這樣做,以保持簡單的例子。

+0

嗨valbertos, 謝謝,但我試過兩個RelativeLayout都是你提出的,但是如果左邊的TextView太長,第一個不會讓右邊的TextView可見。 第二個RelativeLayout對左邊的TextView有一個固定的寬度,因爲它有一個動態的大小,所以這是不正確的。 – louloox 2014-09-06 17:52:51

+0

我沒理解你。再試一次,我修改了我的問題;) – 2014-09-06 19:09:08

+0

好吧,其實看起來你明白我的意思了:)我想根據正確的大小調整左側TextView的寬度,以便右側總是完全顯示1行。但我認爲我可以從xml視圖執行此操作。我會很快嘗試你的解決方案:)謝謝。 – louloox 2014-09-06 20:18:46

0

也許你應該使用的LinearLayout代替的RelativeLayout和使用layout_weight屬性。

<LinearLayout android:orientation="horizontal" ...> 
    <TextView android:id="@+id/txtTitleDestination" android:layout_width="wrap_content" ... /> 
    <TextView android:id="@+id/txtTitleFirstDeparture" android:layout_width="0dp" android:layout_weight="1" ... /> 
</LinearLayout> 
+0

嗨pomber, 嗯,謝謝,這也是我的第一個想法,但它不工作:如果左邊的TextView太長,右邊的不可見... – louloox 2014-09-06 17:56:03

2

我發現了一種使用線性佈局的方法,訣竅是不要忘記width =「wrap_content」爲linearLayout,希望它可以提供幫助。

<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:orientation="horizontal"> 


<TextView 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:ellipsize="end" 
    android:singleLine="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:singleLine="true" /> 

+0

Wonderfull它的工作!無需編碼。謝謝 – jmaffre 2016-02-12 10:37:01