2011-10-29 9 views
1

アンドロイドアプリのレイアウトが必要です。カスタムボタンがあり、ボタンにロードすることはできますが、画面上にレイアウトする方法についてはヘルプが必要です。私はアンドロイド開発の初心者です(私の画面名を見てください)。 .xmlの使い方を勉強しようとしていますAndroidボタンのレイアウト

私は必要なものと既に持っている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を使用することをお勧めします。私はいくつかのコードを含んでいます。あなたがここに何をしようとしているのかを達成しようとする方法はいくつかあります。しかし、私は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に添付されているフレームを取り除いています。この行なしでコードを実行しようとすると、私が話していることを理解するでしょう

2番目 - この行:android:src = "@ drawable/ic_launcher"はイメージとして表示するイメージを宣言するために使用されます。

第3 - ボタンの間にスペースが必要な場合は、これらの各要素の間にパディングを挿入できます。あなたは 'android:padding = xxdp'を使ってそうすることができます

第4 - ボタンに特定のサイズを持たせたい場合は、幅と長さを '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> 

それは参考になりましかもしれない...

関連する問題