2013-02-28 61 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>