2013-10-30 12 views
0

我想創建一個類似於此的xml佈局,但是我的xml代碼沒有解決。我該如何創建一個android xml視圖

enter image description here

這裏是我的xml文件:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_margin="5dp" 
    android:layout_weight="1" 
    android:background="#FFFFFFFF" > 

    <ImageButton 
     android:name = "@+id/switchOriginDest" 
     android:src="@drawable/switchorigdest" 
     android:layout_height = "fill_parent" 
     android:layout_width = "wrap_content" 
     android:layout_alignParentLeft="true" /> 

    <TextView 
     android:id="@+id/Origin" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/rounded_edge" 
     android:padding="6dip" 
     android:hint="@string/mycurrentpos" /> 

    <TextView 
     android:id="@+id/Destination" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/rounded_edge" 
     android:layout_below="@id/Origin" 
     android:hint="@string/desthint" 
     android:padding="6dip" /> 

</RelativeLayout> 

我現在已經是

enter image description here

---------- UPDATE ------------

將TextView的layout_width設置爲固定的數字解決了這個問題。然而,任何人都可以好好回答我的一些跟進問題嗎?

  1. 不是0dp使組件自動佔用剩餘空間。在這種情況下它怎麼不起作用。

  2. 如何製作如示例中所示的紅色和綠色圓圈?

+0

你能告訴你已經得到了當前的結果?它可以幫助看到問題在哪裏。 – Guian

回答

0

試試這個(修改它根據自己的需要):

使用線性佈局:

<?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="wrap_content" 
    android:layout_margin="5dp" 
    android:weightSum="1" 
    android:orientation="horizontal" > 

    <ImageButton 
     android:name="@+id/switchOriginDest" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:src="@drawable/back_arrow" /> 
    <LinearLayout 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     > 
     <TextView 
     android:id="@+id/Origin" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:padding="6dip" 
     android:textSize="30sp" 
     android:hint="mycurrentpos" /> 

    <TextView 
     android:id="@+id/Destination" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="desthint" 
     android:textSize="30sp" 
     android:padding="6dip" /> 
    </LinearLayout> 
</LinearLayout> 

希望這將有助於。

使用相對佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:orientation="horizontal" > 

    <ImageButton 
     android:id="@+id/switchOriginDest" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:src="@drawable/back_arrow" /> 

    <TextView 
     android:id="@+id/Origin" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/switchOriginDest" 
     android:hint="mycurrentpos" 
     android:padding="6dip" 
     android:textSize="30sp" /> 

    <TextView 
     android:id="@+id/Destination" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/Origin" 
     android:layout_toRightOf="@+id/switchOriginDest" 
     android:hint="desthint" 
     android:padding="6dip" 
     android:textSize="30sp" /> 

</RelativeLayout> 
+0

人們應該學會使用RelativeLayout來代替嵌套的LinearLayout。在運行時它將不那麼沉重。一旦修復,他的解決方案應該會更好。 – Guian

0

的layout_width一套做0dp是不是在你的例子清晰。如果您使用重量來管理尺寸,那麼設置它是一個好習慣,但是您不會爲佈局尺寸設置任何重量。

嘗試使用WRAP_CONTENT或者一些固定的大小,看看是否有幫助:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" //here it seems ok, since you set the weight to 1 
    android:layout_margin="5dp" 
    android:layout_weight="1" 
    android:background="#FFFFFFFF" > 

    <ImageButton 
     android:name = "@+id/switchOriginDest" 
     android:src="@drawable/switchorigdest" 
     android:layout_height = "fill_parent" 
     android:layout_width = "50dp" //since you src image is bigger than what you need to display, you can't rely on wrap content here ;) 
     android:layout_alignParentLeft="true" /> 

    <TextView 
     android:id="@+id/Origin" 
     android:layout_width="200dp" //lets try with this size. 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/rounded_edge" 
     android:padding="6dip" 
     android:hint="@string/mycurrentpos" /> 

    <TextView 
     android:id="@+id/Destination" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true" //you probably want to align that on the bottom ? 
     android:background="@drawable/rounded_edge" 
     android:layout_below="@id/Origin" 
     android:hint="@string/desthint" 
     android:padding="6dip" /> 

</RelativeLayout> 

More informations about using 0dp sizes here.

+0

我給圖片添加了大小,因爲您的src圖片看起來比所需的大小要大。 – Guian

+0

是啊顯式設置TextView的寬度解決了這個問題,但我不明白,是不是設置寬度爲0dp使它自動佔據空間剩餘? – Daniel

+0

不,設置爲0dip意味着「大小將由另一個標準管理」,它經常與權重設置爲1一起使用,並且在大多數情況下它會佔用剩餘空間,但這就是導致誤解的原因。 – Guian

相關問題