2015-07-19 110 views
4

之外我希望我的TextView被允许在屏幕的水平(也禁用水平滚动条),像这样的:允许的TextView是屏幕

enter image description here

然而,XML我有现在使之自动适应父里面,像这样的:

enter image description here

代码

<RelativeLayout xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/parentlayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#fff"> 

    <TextView 
     android:layout_marginTop="30px" 
     android:id="@+id/tab1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hello1" 
     android:textColor="#1089F9" 
     android:textSize="40sp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginBottom="5dp"/> 

    <TextView 
     android:layout_marginTop="30px" 
     android:id="@+id/tab2" 
     android:layout_toRightOf="@+id/tab1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hello2" 
     android:textColor="#1089F9" 
     android:textSize="40sp" 
     android:layout_marginLeft="50dp" 
     android:layout_marginBottom="5dp"/> 

    <TextView 
     android:layout_marginTop="30px" 
     android:id="@+id/tab3" 
     android:layout_toRightOf="@+id/tab2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hello3" 
     android:textColor="#1089F9" 
     android:textSize="40sp" 
     android:layout_marginLeft="50dp" 
     android:layout_marginBottom="5dp"/> 

</RelativeLayout> 

如何更改我的代码以获取显示在中的布局第一张图片?只要切断textview,如果它离开父级。

编辑:尝试LinearLayout。它没有工作,要么

<LinearLayout xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/parentlayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#fff" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_marginTop="30px" 
     android:id="@+id/tab1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hello1" 
     android:textColor="#1089F9" 
     android:textSize="40sp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginBottom="5dp"/> 

    <TextView 
     android:layout_marginTop="30px" 
     android:id="@+id/tab2" 
     android:layout_toRightOf="@+id/tab1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hello2" 
     android:textColor="#1089F9" 
     android:textSize="40sp" 
     android:layout_marginLeft="50dp" 
     android:layout_marginBottom="5dp"/> 

    <TextView 
     android:layout_marginTop="30px" 
     android:id="@+id/tab3" 
     android:layout_toRightOf="@+id/tab2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hello3" 
     android:textColor="#1089F9" 
     android:textSize="40sp" 
     android:layout_marginLeft="50dp" 
     android:layout_marginBottom="5dp"/> 

</LinearLayout> 
+0

机器人:单线= textView上的“true”可能会诀窍 –

+1

哦,是的,它确实!很简单的解决方案请将其张贴为答案。它也许可以帮助其他人在截断文本后显示“....”的追踪者btw。我如何阻止这种情况发生? – Dinuka

+0

切断文本后,显示“....”。我如何阻止? – Dinuka

回答

0

尝试使用这个属性对你3 TextView的

android:maxLines="1" 

设置MAXLINES后= “1”,我得到这个结果

enter image description here

+0

你天才!这工作:D – Dinuka

+0

乐于帮助:D –

+1

顺便说一句,这也适用于左侧的工作?我的意思是,如果我要切断Hello1,它会起作用吗? – Dinuka

0

只需使用LinearLayout代替RelativeLayout并设置定向水平。

EDITED

<LinearLayout xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/parentlayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#fff" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/tab1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hello1" 
     android:textColor="#1089F9" 
     android:textSize="40sp" 
     android:maxLines="1" 
     android:layout_marginLeft="20dp" 
     android:layout_marginBottom="5dp"/> 

    <TextView 
     android:layout_marginTop="30px" 
     android:id="@+id/tab2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hello2" 
     android:textColor="#1089F9" 
     android:textSize="40sp" 
     android:maxLines="1" 
     android:layout_marginLeft="50dp" 
     android:layout_marginBottom="5dp"/> 

    <TextView 
     android:layout_marginTop="30px" 
     android:id="@+id/tab3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hello3" 
     android:textColor="#1089F9" 
     android:textSize="40sp" 
     android:maxLines="1" 
     android:layout_marginLeft="50dp" 
     android:layout_marginBottom="5dp"/> 
</LinearLayout> 
+0

我担心如果使用LinearLayout,我将无法使用android:layout_toRightOf =“@ + id/tab1”属性。有没有办法解决这个问题? – Dinuka

+0

看起来像LinearLayout不会创建我想要的结果。仍然适合父视图中的儿童 – Dinuka

+0

如果将方向设置为水平,则视图将自动设置为正确 – chandil03