2014-12-02 108 views
0

我有個親戚佈局包括兩個TextViews:相對佈局 - 長的TextView重疊

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.example.pris.ViewActivity" 
    tools:ignore="MergeRootFrame"> 

<TextView 
    android:id="@+id/description" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/myDes" /> 

<TextView 
    android:id="@+id/tex" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/description" 
    android:text="@string/myText" /> 

<AutoCompleteTextView 
    android:id="@+id/textfield" 
    android:layout_width="200dip" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/tex" 
    android:completionThreshold="1" 
    android:inputType="text" /> 

<Button 
    android:id="@+id/but" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="@string/suchen" 
    android:layout_toRightOf="@+id/textfield" 
    android:layout_alignParentRight="true" 
    android:onClick="addToList" /> 

<Button 
    android:id="@+id/but2" 
    android:layout_weight="1.0" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="Liste löschen" 
    android:layout_below="@+id/ex" 
    android:onClick="removeList" /> 

<ListView 
    android:id="@+id/ex" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_weight="1.0" 
    android:choiceMode="singleChoice" 
    android:layout_below="@+id/but"> 
</ListView> 
</RelativeLayout> 

我只插入「說明」近日,文字有相當長的 - 現在它與它下面的一切重疊。

我該如何改變這種情況?

如果可能,我不想更改整個佈局,但不幸的是不能縮短文本。 enter image description here

+3

在這種情況下,圖像或屏幕截圖會更好,更容易讓其他人幫助您。 – 2014-12-02 07:59:58

+3

請添加此屏幕截圖 – Lokesh 2014-12-02 08:00:29

+0

請使用線性佈局,而不是相對的 – 2014-12-02 08:47:38

回答

0

我解決了它。

其實很簡單。

我把我原來的佈局,並補充說:

android:scrollHorizontally="true" 

到第一TextView的。發現在TextVew的API上。

就是這樣。

1

你有沒有試過把ScrollView放在RelativeLayout上面?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</ScrollView> 

此外,檢查.XML結束,你缺少結束標記

+0

我試過了,謝謝,沒有幫助。 – user1862770 2014-12-02 22:40:10

1

修訂

,你必須爲你的按鈕機器人提供定位:layout_toRightOf =「@ + ID /文本框」

檢查

<Button 
      android:id="@+id/but" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="tex" 
      android:layout_below="@id/tex" 
      android:layout_toRightOf="@+id/textfield" 
      android:layout_alignParentRight="true" 
      android:onClick="addToList" /> 

這裏是佈局

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


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:id="@+id/description" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/dumy" /> 

     <TextView 
      android:id="@+id/tex" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/description" 
      android:text="tex" /> 

     <AutoCompleteTextView 
      android:id="@+id/textfield" 
      android:layout_width="200dip" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/tex" 
      android:completionThreshold="1" 
      android:inputType="text" /> 

     <Button 
      android:id="@+id/but" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="tex" 
      android:layout_below="@id/tex" 
      android:layout_toRightOf="@+id/textfield" 
      android:layout_alignParentRight="true" 
      android:onClick="addToList" /> 

     <Button 
      android:id="@+id/but2" 
      android:layout_weight="1.0" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="Liste löschen" 
      android:layout_below="@+id/ex" 
      android:onClick="removeList" /> 

     <ListView 
      android:id="@+id/ex" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_weight="1.0" 
      android:choiceMode="singleChoice" 
      android:layout_below="@+id/but"></ListView> 
    </RelativeLayout> 
</ScrollView> 

和截圖全碼

result

+1

你會如何幫助OP?你可以用你的答案給出任何解釋,而不是僅僅發放一些代碼。 – 2014-12-02 08:10:08

+0

如果我能我可以downvote這完全崩潰我的佈局路徑。 – user1862770 2014-12-02 22:39:49

+1

你是什麼意思墜毀......?它只是一個佈局,如果你不想讓你的文本重疊其他文本,只需使用linearlayout作爲父佈局,然後設置方向爲垂直,即時只是試圖幫助你:) 謝謝 – 2014-12-03 02:54:57

1

讓你的文字滾動通過運行時增加以下功能吧:

TextView description = (TextView) findViewById(R.id.description); 
description.setSingleLine(); 
description.setEllipsize(TruncateAt.MARQUEE); 
description.setSelected(true); 

更新:

所以,你必須創建一個自定義視圖:

首先,創建以下類:

import android.content.Context; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.widget.TextView; 

public class ScrollingTextView extends TextView { 

    public ScrollingTextView(Context context, AttributeSet attrs, 
      int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public ScrollingTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public ScrollingTextView(Context context) { 
     super(context); 
    } 

    @Override 
    protected void onFocusChanged(boolean focused, int direction, 
      Rect previouslyFocusedRect) { 
     if (focused) { 
      super.onFocusChanged(focused, direction, previouslyFocusedRect); 
     } 
    } 

    @Override 
    public void onWindowFocusChanged(boolean focused) { 
     if (focused) { 
      super.onWindowFocusChanged(focused); 
     } 
    } 

    @Override 
    public boolean isFocused() { 
     return true; 
    } 
} 

然後,在你的XML佈局定義TextView將其定義方式如下:

<!-- use the path of the package which you put the class ScrollingTextView in --> 
<com.example.ScrollingTextView 
    android:id="@+id/description" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/myDes" 
    android:ellipsize="marquee" 
    android:lines="1" 
    android:marqueeRepeatLimit="marquee_forever" 
    android:singleLine="true"/> 
在活動聲明時

則:

com.example.ScrollingTextView description = (com.example.ScrollingTextView) findViewById(R.id.description); 
+0

截圖是在使用您的代碼片段之後。真的沒有幫助 – user1862770 2014-12-02 08:34:08

+0

@ user1862770看看我的更新回答 – 2014-12-02 08:51:53

+0

我下班後嘗試 - 但是認真的是,沒有更簡單的解決方案可以將長文本轉換爲textview而沒有重疊?我不能成爲第一個遇到這個問題的人! – user1862770 2014-12-02 09:06:29