[英]Layout of Android Buttons
我需要某個Android應用程序的特定布局。 我有自定義按鈕,我可以將它們加載到按鈕,但需要幫助,如何將它們放在屏幕上。 我是android開發的新手(請參閱我的屏幕名稱)。 嘗試學習如何使用.xml
我已經附加了我需要的東西以及我已經擁有的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>
嘗試這個..
<?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>
它可能對你有幫助..
我看到你試圖將圖像顯示為按鈕。 因此,我建議您使用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'來實現
第四 - 如果您希望按鈕具有特定大小,您可以通過將'wrap_content'替換為'xxdp'作為寬度和高度來實現。 但是,在您打算執行此操作時添加以下行非常重要:android:scaleType =“fitXY”。 這將告訴元素您希望以絕對方式布局它。 但是,我建議您在將圖像放入布局之前適當調整圖像大小,而不是采用這種方法。
要在屏幕上填充此內容,您需要將其設置為某個活動的視圖。 為此,您可以參考以下代碼。
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.