2014-08-31 35 views
0

美好的一天每個人,目前我試圖讓我的第一個Android應用程序,然後我意識到的第一件事,那個愚蠢的XML UI設計。Android用戶界面:大小2視圖?

我有這2個視圖(按鈕),我想使他們如此第一個填充父母的一半(RelativeLayout)和第二個填充父母的另一半... 我的代碼是:

<Button 
     android:id="@+id/Top1" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@id/Top2" 
     android:text="top1"/> 
    <Button 
     android:id="@+id/Top2" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@id/Top1" 
     android:text="TOP2"/> 

的問題,我是得到這個錯誤: 「沒有資源的發現,給定的名稱相匹配(具有值在'layout_toLeftOf‘@ ID /最上方的兩個’)。」

看來,甚至連Android都不想使用XML。 像老C程序中,如果一個方法是寫調用下面,它會給出一個錯誤...

所以我有2個問題:1 :如何解決在XML這個問題? 2:或者我可以避免這種XML設計,並使用像C#一樣的類似代碼的設計?

+0

你試過了Android Studio的框架?在大多數情況下,您甚至不需要觸摸XML代碼。只需拖放,調整大小。 – 2016-04-07 05:16:28

回答

0

你是對的,Top2需要首先在XML中定義,然後才能引用它。話雖如此,如果你所做的只是把2個按鈕放在一起,並讓它們填滿父母,你應該考慮使用LinearLayout。秩序在那裏也很重要:對於水平方向,孩子們從左到右擺放,因爲垂直襬放從上到下。

+0

Soo ...有沒有辦法,我在XML中製作一個「聲明字段」,然後組織? 就像在帕斯卡爾一樣,事情首先被宣佈,然後使用它們? – Rabir 2014-08-31 13:48:22

1

有幾個方法,你可以去了解這一點,我向你們提供兩種方式並排:

  1. 使用帶有方向的LinearLayout中設置爲橫向到有按鍵排列側

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="horizontal" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:id="@+id/btn_container"> 
    
        <Button 
         android:layout_width="0dp" 
         android:layout_height="wrap_content" 
         android:text="Left Button" 
         android:layout_weight="1" 
         android:id="@+id/btn_left" /> 
    
        <Button 
         android:layout_width="0dp" 
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:text="Right Button" 
         android:id="@+id/btn_right" /> 
    </LinearLayout> 
    
  2. 使用RelativeLayout和一個額外的視圖來調整你的按鈕。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 
    
        <View 
         android:id="@+id/strut" 
         android:layout_width="0dp" 
         android:layout_height="0dp" 
         android:layout_centerHorizontal="true" /> 
    
        <Button 
         android:id="@+id/btn_left" 
         android:layout_width="0dp" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_alignRight="@id/strut" 
         android:text="Left Button" /> 
    
        <Button 
         android:id="@+id/btn_right" 
         android:layout_width="0dp" 
         android:layout_height="wrap_content" 
         android:layout_alignLeft="@id/strut" 
         android:layout_alignParentRight="true" 
         android:text="Right Button" /> 
    </RelativeLayout> 
    
+0

謝謝... 這將解決第一個問題......第二個將更難在這個廢話XML desginer ... – Rabir 2014-08-31 15:50:10

+0

這隻需要一些時間習慣於使用XML設計。或者,你可以在代碼中完成所有上述操作,但我並不喜歡在代碼中進行設計工作。 – michaelcarrano 2014-08-31 19:32:43

+0

我喜歡更多的代碼設計方式,我認爲它給了我更多的優勢... 大多數「編譯器」類型工作不像「解釋器」類似... – Rabir 2014-09-02 14:55:29