2013-04-29 87 views
0

我奠定了它呈現在ListView搜索結果的應用程序。我定義了每個項目有一個自定義佈局如下:Android RelativeLayout - 當空間不足時顯示物品,但在空間不足時顯示在單獨的行上?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="5dp" 
    android:paddingRight="?android:attr/scrollbarSize" > 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:ellipsize="end" 
     android:textAppearance="?android:attr/textAppearanceSearchResultTitle" 
     android:textIsSelectable="false" /> 

    <TextView 
     android:id="@+id/date" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/title" 
     android:layout_toRightOf="@+id/subtitle" 
     android:gravity="bottom|right" 
     android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle" 
     android:textIsSelectable="false" /> 

    <TextView 
     android:id="@+id/subtitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/title" 
     android:gravity="bottom|left" 
     android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle" 
     android:textIsSelectable="false" /> 

</RelativeLayout> 

這看起來偉大的,當字幕和日期是適當的長度,使其在同一行的,但是,如果字幕消耗大部分的看起來很恐怖線和力日期採取非常薄的寬度,所以垂直包裹。

我希望做的是讓他們出現並排的一面,當有空間,但在不同的行如果沒有。我試着擺弄各種layout_ *屬性和重力無濟於事,這個問題不是很有Google能力(至少,我想不出合適的詞來搜索)。任何人都可以指向我需要實現的佈局規則的組合嗎?或者如果一個更合適,也許是一個不同的容器?

回答

1

我相信下面的代碼會做你想要什麼,但我看不到的方式只能在xml佈局中執行此操作。 基本上我添加了一個textChangedListener到具有兩個不同的佈局選項,我相信你正在尋找兩個不同的TextViews,裏面都帶有日期顯示的TextView自己的相對佈局。當字幕被設置時,其中的第一個用於保存文本,如果它需要多於一行的話,則使用第二個TextView,並且在另一種情況下,另一個將其可見性選項設置爲GONE。 在我的例子中,我使用了一個單獨的線程來改變字幕,希望這不會讓事情混淆太多。

佈局XML如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:ellipsize="end" 
    android:textAppearance="?android:attr/textAppearanceSearchResultTitle" 
    android:background="#FF009999" 
    android:textIsSelectable="false" 
    android:text="@string/my_title" /> 
<RelativeLayout 
    android:id="@+id/resizingTextContainer" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/title" > 
    <TextView 
     android:id="@+id/date" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle" 
     android:textIsSelectable="false" 
     android:textColor="#FFFFFFFF" 
     android:background="#FF000000" /> 
    <TextView 
     android:id="@+id/subtitle1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@+id/date" 
     android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle" 
     android:textIsSelectable="false" 
     android:textColor="#FFFFFF" 
     android:background="#FF00FF00" /> 
    <TextView 
     android:id="@+id/subtitle2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/date" 
     android:textAppearance="?android:attr/textAppearanceSearchResultSubtitle" 
     android:textIsSelectable="false" 
     android:visibility="gone" 
     android:textColor="#FFFFFF" 
     android:background="#FF00FF00" />  
</RelativeLayout> 
<Button 
    android:id="@+id/startTestBtn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/resizingTextContainer" 
    android:layout_centerHorizontal="true" 
    android:text="Click To Begin" 
    /> 
    </RelativeLayout> 

和主要活動代碼與TextView的切換邏輯:

package com.example.code.examples.changelayoutwithtextlength; 

    import android.os.Bundle; 
    import android.app.Activity; 
    import android.text.Editable; 
    import android.text.TextWatcher; 
    import android.view.Menu; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.TextView; 

    public class MainActivity extends Activity { 

TextWatcher textChangeDisplayCheck = new TextWatcher(){ 
    @Override 
    public void afterTextChanged(Editable s) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
     displayLatestSubtitle(s);   
    }   
}; 

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

    TextView DateTextView = (TextView)findViewById(R.id.date); 
    DateTextView.setText("29th April 2013"); 

    Button b = (Button)findViewById(R.id.startTestBtn); 
    b.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) 
     { 
      v.setEnabled(false); 
      Thread thread = new testSubtitleThread(); 
      thread.start(); 
     }   
    }); 

    TextView tv = (TextView)findViewById(R.id.subtitle1); 
    tv.addTextChangedListener(textChangeDisplayCheck); 
    TextView tv2 = (TextView)findViewById(R.id.subtitle2); 
    tv2.addTextChangedListener(textChangeDisplayCheck); 
} 

private void displayLatestSubtitle(CharSequence newSubtitle) 
{ 
    TextView tv = (TextView)findViewById(R.id.subtitle1); 
    TextView tv2 = (TextView)findViewById(R.id.subtitle2); 

    tv.removeTextChangedListener(textChangeDisplayCheck); 
    tv2.removeTextChangedListener(textChangeDisplayCheck); 

    tv.setVisibility(View.GONE); 
    tv2.setVisibility(View.GONE); 
    tv2.setText(""); 
    tv.setText(newSubtitle); 

    if(tv.getLineCount() > 1) 
    { 
     tv.setText(""); 
     tv2.setText(newSubtitle); 
     tv2.setVisibility(View.VISIBLE); 
    } 
    else 
    { 
     tv.setVisibility(View.VISIBLE); 
    } 

    tv.addTextChangedListener(textChangeDisplayCheck); 
    tv2.addTextChangedListener(textChangeDisplayCheck); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public class testSubtitleThread extends Thread 
{ 
    String[] subtitles = new String[] { "a short one", "a really long winded subtitle that will take over more than the allowed space", "tiny", 
      "Really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, long.", 
      ".....", "text just to long to fit on my device"}; 

    private android.os.Handler handler = new android.os.Handler() 
    { 
     @Override    
     public void handleMessage(android.os.Message msg) 
     {     
      if(msg.what < subtitles.length) 
      { 
       TextView tv = (TextView)findViewById(R.id.subtitle1); 
       tv.setText(subtitles[msg.what]); 
      } 
      else 
      { 
       Button b = (Button)findViewById(R.id.startTestBtn); 
       b.setEnabled(true); 
      } 
     } 
    }; 
    @Override 
    public void run() 
    { 
     for(int i = 0; i <= subtitles.length; i++) 
     { 
      handler.sendEmptyMessage(i);  
      try 
      { 
       Thread.sleep(3000); 
      } 
      catch (InterruptedException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }       
     } 
    } 
} 
    } 

我希望這有助於。

+0

好像是有一個解決方案,所以我會接受。可悲的是這似乎是有點faff的只是爲了讓我去的效果,所以我代替設置'機器人:MAXLINES =「1」'上的日期和TextView的設置字幕是'機器人:layout_toLeftOf = 「@ + id/date」'並刪除日期的'android:layout_toRightOf =「@ + id/subtitle」'。這意味着日期始終位於右側的一行上,並且填充可以使字幕適合左側的位置(可能會消耗多行)。無論如何,謝謝你對我的問題的解決方案:) – 2013-04-30 10:38:18

0

在你的佈局,我將定義每一項的兩個視圖(字幕和日期),一個在同一直線上,一個在下面一行。 然後我會檢查這些2場(或它們的總和)的長度,我會寫這段代碼:

if (length > MAX_LENGTH_OF_LINE) > { 
     findViewById(R.id.subtitle_line_below).setVisibility(View.VISIBLE); 
     findViewById(R.id.subtitle_same_line).setVisibility(View.GONE); 
    } else { 
     findViewById(R.id.subtitle_line_below).setVisibility(View.GONE); 
     findViewById(R.id.subtitle_same_line).setVisibility(View.VISIBLE); 
    } 
相關問題