1
我試圖創建假LCD顯示。只在其等寬字母寬度的步驟中居中文本視圖
應該存在一些正常的文字,呈現出一些數據,和背景,僞造的是LCD的外觀:
我寫了這個XML,對準對方的前3個TextViews。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/footer_linearLayout_footer_parent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/dashboard_bottom_background"
android:gravity="center"
android:padding="3dip" >
<TextView
android:id="@+id/textView_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="none"
android:lines="1"
android:text="text"
android:textColor="@color/holo_blue_light"
android:textSize="28dip" />
<TextView
android:id="@+id/textView_heading2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.15"
android:ellipsize="none"
android:gravity="center"
android:lines="1"
android:text="88888888888888888888888888888888888"
android:textColor="@color/holo_blue_light"
android:textSize="28dip" />
<TextView
android:id="@+id/textView_heading3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.15"
android:gravity="center"
android:lines="1"
android:text="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
android:textColor="@color/holo_blue_light"
android:textSize="28dip" />
</RelativeLayout>
正常工作對某些情況下,但現在看,如果的最高觀的變化長度限制:Textlength會發生什麼:
的意見是沒有正確對齊任何更多,因爲只有上查看更改了它寬度,因此再次居中。
我所問的是,有居中一個TextView的可能性,但只有在使用monospace
-font的一個字母的寬度的步驟
感謝您的輸入。但是,通過該代碼,文本具有左對齊。但是,讓它集中是沒有什麼大不了的。這種解決方案的缺點是,數字的數量不再是可變的,不能依賴於屏幕大小。我會研究它,如果我可以使用它,也許有一些修改。 – msal 2012-01-17 13:22:02
@walla:如果您想要,可以根據屏幕大小在運行時將'TextView'添加到'LinearLayout'。 另一種解決方案是根據Android的需要,使用不同數量的數字[支持多個屏幕](http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts)不同的XML佈局:例如,您可以對於7英寸平板電腦具有5位數字的'res \ layout-sw600dp \ lcd_layout.xml',對於10英寸平板電腦具有8位數字的'res \ layout-sw720dp \ lcd_layout.xml'。 – 2012-01-17 21:11:21
好點。我會考慮到這一點。 – msal 2012-01-18 12:18:41