歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android TextView一行只顯示一個文字

Android TextView一行只顯示一個文字

日期:2017/3/1 10:14:57   编辑:Linux編程

項目需要實現這樣一個效果.

一開始覺得挺簡單的,不就是設置一個TextView的Android:ems="1"屬性嗎.設置完成,運行之後才發現,我的TextView每行顯示的是2個文字.

然後就在網上找資料.但資料很少.我也不知道是大家在平時碰到這種效果機會少呢還是這個效果就是菜鳥級別,入門就懂的.不管是哪種,我都把我實現的方法給記錄下來吧.

話說,就在我焦頭爛額還是沒能解決這個問題時,我跟部門設計同事說:"你設計的這個圖片太大了,造成一行最少也要顯示2個文字".

他告訴我一個方法,一語驚醒夢中人.然後感慨,我們設計的思維都要比我厲害.囧哪~

進入正題:

最後解決辦法是:將TextView包裹在一個LinearLayou中,LinearLayout設置背景圖片,TextView限定高和寬,問題圓滿解決.其實說白了就是TextView的寬度問題.一開始我設置的TextView高和寬是wrap_content這樣就造成了兩個文字才自動換行.但是限定寬度之後,一行只能顯示一個文字,因此,TextView就實現了一行只顯示一個文字的效果了.

布局代碼:

<LinearLayout

android:id="@+id/main_cooperation"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:layout_marginBottom="60dp"

android:background="@drawable/commercial_right02"

android:gravity="center_vertical" >


<TextView

android:id="@+id/main_cooperation_tv"

android:layout_width="20dp"

android:layout_height="100dp"

android:layout_marginLeft="10dp"

android:text="@string/main_cooperation"

android:textColor="@color/main_category_4"

android:textSize="@dimen/main_category_tx_size2" />

</LinearLayout>

Copyright © Linux教程網 All Rights Reserved