2013-02-28 65 views
2

我很新的Android和我要實現一個視圖它看起來像:如何在android中的中心繪製一條線以及一些文字?

-------------Some Text---------------------- 

我知道,我可以借鑑使用類似的代碼用我的XML View行:

<View 
android:layout_width="100dp" 
android:layout_height="1dp" 
android:background="#00ff00" /> 

這將在繪製結束:

----------------------- 

但畢竟這我想放置文本,並再次畫線的另一端。

我試過,但我得到的是這樣的:

---------------------- 
text 

當我View代碼後加一些TextView

這有可能做什麼我的目標?

在此先感謝。

回答

3

可能,這將幫助你得到你想要的。給予權重的觀點是關鍵..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:gravity="center" 
android:orientation="horizontal" > 

<View 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
    android:layout_weight="1" 
    android:background="#00ff00" /> 

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

<View 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
    android:layout_gravity="center" 
    android:layout_weight="1" 
    android:background="#00ff00" /> 
</LinearLayout> 
+1

這工作得很好,但我建議使用一個'RelativeLayout',因爲想必這佈局將有其他元素太,這意味着你必須嵌套'LinearLayout'。這對性能不利。 – j0ntech 2013-02-28 10:54:25

+0

@ j0ntech是的,但是在相對佈局中,您必須給定固定寬度。我不知道任何其他方式來拉伸線條。 – SkyWalker 2013-02-28 11:07:03

+0

@Hardik爲什麼修好?如果從左向右設置對齊,則會根據父寬度進行設置 – 2013-02-28 11:11:30

0

好,最簡單的很可能只是有一個RelativeLayout的或水平的LinearLayout,一半的生產線的寬度和創建它;

視圖(50dp) - TextView的 - 視圖(50dp)

1

試試這個..

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <View 
     android:id="@+id/view1" 
     android:layout_width="60dp" 
     android:layout_height="4dp" 
     android:layout_marginTop="5dp" 
     android:background="#00ff00" /> 

    <TextView 
     android:id="@+id/tv1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="2dp" 
     android:layout_marginRight="2dp" 
     android:layout_toRightOf="@+id/view1" 
     android:text="My Text" /> 

    <View 
     android:id="@+id/view2" 
     android:layout_width="60dp" 
     android:layout_height="4dp" 
     android:layout_marginTop="5dp" 
     android:layout_toRightOf="@+id/tv1" 
     android:background="#00ff00" /> 

</RelativeLayout> 

希望它爲你..

讓我知道,如果它的工作原理。

2

嘗試使用FrameLayout此情況下,或RelativeLayout防止深視圖層次,提高性能

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#fff" > 

<View 
    android:layout_width="200dp" 
    android:layout_height="1dp" 
    android:layout_gravity="center" 
    android:background="#555" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:background="#fff" 
    android:text="Hello world" /> 

</FrameLayout>