如何在同一行中的Android

问题描述:

显示2个textviews

我有布局xml文件:如何在同一行中的Android

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TextView 
    android:id="@+id/titlename" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/HostName" 
android:layout_weight="0" 

/> 
<TextView 
    android:id="@+id/name" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="0" 
/> 
</LinearLayout> 

当我执行上述一个,我的输出是象下面这样:

enter image description here

但我的要求是让我的输出如下:

| text1: text2 | 

任何人都可以帮助吗?

+0

你有没有尝试android:layout_weight =“0.5”两个文本视图?这会给两个半宽? – 2013-04-23 18:09:44

使用下面的代码

enter image description here

更新

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:weightSum="1" > 
<TextView 
    android:id="@+id/titlename" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:gravity="center" 
    android:text="hi" /> 
<TextView 
    android:id="@+id/name" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:gravity="center" 
    android:text="2hi2" /> 
</LinearLayout> 

我会拿出weight属性。这是我能看到的唯一的事情就是搞砸了。还要注意的是fill_parent已被废弃,你应该使用match_parent代替

或者,如果你需要的weight出于某种原因,然后改变各为1,如果你想让他们各自占据半个屏幕。

此外,如果使用weight那么你应该设置android:layout_width="0dp"

设置android:orientation="horizontal"您的LinearLayout。

+0

它被设置为“水平” – codeMagic 2013-04-23 18:13:21

像下面那样更新布局,除非在父LinearLayout上设置weightSum,否则不需要包含weight = 0属性。加上重量和weightSum并不真正适合你在找什么。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView 
     android:id="@+id/titlename" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/HostName" 

     /> 
    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 
</LinearLayout> 

你可以尝试使用RelativeLayout的

这样,您可将其设置在alignParentTop="true"元素TITLENAME并设置名称alignTo="@id/titlename"toRightOf="@id/titlename"在名称元素

有Android版本:layout_width为0dp。然后为每个文本视图设置0.5个权重。每个textview将填充父布局的一半宽度。例如:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 
<TextView 
    android:id="@+id/titlename" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:text="@string/HostName" 
android:layout_weight="0.5" 

/> 
<TextView 
    android:id="@+id/name" 
android:layout_width="0dp" 
android:layout_height="wrap_content" 
android:layout_weight="0.5" 
/> 
</LinearLayout> 

其实您的原始代码在我的ADT中完美工作。您是否更改了更新第二个textview的Java代码中的文本大小或行填充?

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

<TextView 
    android:id="@+id/titlename" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/HostName" 
/> 
<TextView 
    android:id="@+id/name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
/> 
</LinearLayout>