2011-10-29 184 views
1

我需要某個android應用程序的特定佈局。我有自定義按鈕,可以將它們加載到按鈕中,但需要有關如何將它們放在屏幕上的幫助。我是Android開發的新手(查看我的屏幕名稱)。試圖學習如何使用.xmlAndroid按鈕的佈局

我附加了我需要的和我已經擁有的xml代碼。任何幫助將不勝感激。

<?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="vertical" android:weightSum="1"> 


       <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal" android:weightSum="1" 
       android:gravity="center_vertical|center_horizontal" > 
        <Button android:gravity="center_vertical|center_horizontal" android:id="@+id/button" android:layout_height="wrap_content" android:padding="10dp" android:background="@drawable/schedule_button" android:layout_width="wrap_content"></Button> 
        <Button android:id="@+id/button" android:layout_height="wrap_content" android:padding="10dp" android:background="@drawable/grad_button" android:layout_width="wrap_content"></Button> 
       </LinearLayout> 


       <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal" android:weightSum="1" 
       android:gravity="center_vertical|center_horizontal" > 
       <Button android:gravity="center_vertical|center_horizontal" android:id="@+id/button" android:layout_height="wrap_content" android:padding="10dp" android:background="@drawable/schedule_button" android:layout_width="wrap_content"></Button> 
       <Button android:id="@+id/button" android:layout_height="wrap_content" android:padding="10dp" android:background="@drawable/grad_button" android:layout_width="wrap_content"></Button> 
       </LinearLayout> 



</LinearLayout> 

Her is the layout

回答

1

我看到你想顯示的圖像作爲按鈕。因此,我建議您使用ImageButton而不是Button。我已經包含了一些代碼。有幾種不同的方式可以嘗試實現你打算在這裏做什麼。但是,我必須選擇僅使用LinearLayout來保持簡單。 Deepa已經使用了RelativeLayout來實現這一點,這確實是一個非常好的解決方案。您應該查看RelativeLayout,因爲它可能在構建佈局時爲您提供幫助。

<?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:gravity="center" 
android:orientation="vertical" > 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <ImageButton 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src = "@drawable/ic_launcher" 
     android:background="@null"/> 
    <ImageButton 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src = "@drawable/ic_launcher" 
     android:background="@null"/> 
</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" >  
    <ImageButton 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:src = "@drawable/ic_launcher" 
     android:background="@null" 
     android:layout_height="wrap_content"/> 

    <ImageButton 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:src = "@drawable/ic_launcher" 
     android:background="@null" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" >  

    <ImageButton 
     android:id="@+id/button5" 
     android:layout_width="wrap_content" 
     android:src = "@drawable/ic_launcher" 
     android:layout_height="wrap_content" 
     android:background="@null"/> 

    <ImageButton 
     android:id="@+id/button6" 
     android:layout_width="wrap_content" 
     android:src = "@drawable/ic_launcher" 
     android:layout_height="wrap_content" 
     android:background="@null"/> 
</LinearLayout> 

在上面的代碼中,我使用了默認的啓動圖標的圖像。您必須根據需要將它們替換爲相應的按鈕圖像。此外,您必須瞭解這裏有4個重要的方面。

首先 - 我已經使用android:background =「@ null」來擺脫連接到ImageButton的框架。嘗試運行沒有這條線的代碼,你會明白我在說什麼

第二行 - 這行:android:src =「@ drawable/ic_launcher」用來聲明你顯示的圖像作爲圖像。

第三 - 如果你想在按鈕之間留出一些空間,你可以在每個元素之間加入一些填充。你可以通過使用'android:padding = xxdp'來做到這一點。第四 - 如果你想讓按鈕具有特定的大小,你可以通過用'xxdp'替換值'wrap_content'高度。但是,當您打算這麼做時,添加以下行是非常重要的:android:scaleType =「fitXY」。這將告訴元素,你希望它以絕對的方式佈局。但是,我不建議採用這種方法,而是在將它們放入佈局之前,適當調整圖像的大小。

+0

謝謝,那正是我需要的。感謝您的詳細解釋。 – Bryan1234321

+0

不客氣。祝你好運! – Abhijit

0

要填充這個,你需要設置某些活動的在屏幕上查看。爲此你可以參考下面的代碼。

public class ButtonLayout extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // You can put your XML file name here 
     setContentView(R.layout.main); 
} 
} 
+0

我沒有java類的問題,而是在xml文件中的佈局。任何幫助如何設置XML文件? – Bryan1234321

+0

設置XML的意思..你想拖放像控制易於開發? –

2

試試這個..

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical|center_horizontal"> 
<Button 
    android:id="@+id/Button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Schedules"/> 

<Button 
    android:id="@+id/Button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
android:layout_toRightOf="@id/Button1" 
android:text="Grand Nite" 
/> 

<Button 
android:id="@+id/Button3" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@id/Button1" 
android:text="The paper"/> 
<Button 
android:id="@+id/Button4" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_toRightOf="@id/Button3" 
android:layout_below="@id/Button1" 
android:text="The Splash" 
/> 

<Button 
android:id="@+id/Button5" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@id/Button3" 
android:text="Facebook"/> 
<Button 
android:id="@+id/Button6" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_toRightOf="@id/Button5" 
android:layout_below="@id/Button3" 
android:text="Twitter" 
/> 
</RelativeLayout> 

它可以幫助你..