2013-04-23 67 views
6
顯示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

回答

15

使用下面的代碼

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> 
0

我會拿出weight屬性。這是我能看到的唯一的事情就是搞砸了。還要注意的是fill_parent已被廢棄,你應該使用match_parent代替

或者,如果你需要的weight出於某種原因,然後改變各爲1,如果你想讓他們各自佔據半個屏幕。

此外,如果使用weight那麼你應該設置android:layout_width="0dp"

0

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

+0

它被設置爲「水平」 – codeMagic 2013-04-23 18:13:21

0

像下面那樣更新佈局,除非在父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> 
0

你可以嘗試使用RelativeLayout的

這樣,您可將其設置在alignParentTop="true"元素TITLENAME並設置名稱alignTo="@id/titlename"toRightOf="@id/titlename"在名稱元素

0

有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> 
0

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

0
<?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> 
相關問題