2015-10-13 102 views
0

我是新來的android,我想用2按鈕做一個MainActivity屏幕,1是「對於父母」,1是「爲兒童」。到目前爲止,我的佈局如下:其他按鈕下的背景圖像

<?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"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingBottom="10dip" 
     android:text="For parent" 
     android:id="@+id/parent" 
     android:layout_gravity="center_horizontal" /> 

    <Button 
     android:paddingBottom="10dip" 
     android:text="For parent" 
     android:id="@+id/children" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_centerInParent="true"/> 
</LinearLayout> 

我有,我想這是這次活動的背景的背景圖像,以及2個按鈕,將在它的頂部。我如何存檔它?以及背景圖片的大小應該是多少?

謝謝

回答

0

你應該看看Drawable Resources在Android開發者。他們解釋說

位圖文件是.png,.jpg或.gif文件。 Android將它們保存在res/drawable /目錄中時,Android爲這些文件中的任何一個創建Drawable資源。

所以,你應該導入你的圖像,並把它放在你的/ res/drawable /文件夾中。請記住不同的屏幕尺寸。

您正在查找的android xml符號被稱爲android:background。在你的代碼,它應該是這樣的:

<?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:background="@drawable/your_background" 
    android:orientation="vertical"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingBottom="10dip" 
     android:text="For parent" 
     android:id="@+id/parent" 
     android:layout_gravity="center_horizontal" /> 

    <Button 
     android:paddingBottom="10dip" 
     android:text="For parent" 
     android:id="@+id/children" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_centerInParent="true"/> 
</LinearLayout> 

另一種選擇,是加入ImageView,填補了整個背景(但這個ImageView的是通過其母公司的LayoutParams影響,因爲保證金爲例)。使用ImageView將爲您提供更多處理圖像的自由,例如將ScaleType設置爲適合佈局的中心。

0

它可以像下面(的ImageView被拉伸到整個視圖和重新設置,所以它會像背景圖片):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="your_drawable"/> 


    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingBottom="10dip" 
     android:text="For parent" 
     android:id="@+id/parent" 
     android:layout_gravity="center_horizontal" /> 

    <Button 
     android:paddingBottom="10dip" 
     android:text="For children" 
     android:id="@+id/children" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_centerInParent="true"/> 

</RelativeLayout> 
0

設置的背景屬性您根ViewGroup。

更改爲

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

這個

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@drawable/ic_launcher" 
     android:scaleType = "centerCrop"   
     android:orientation="vertical"> 
0

你需要做的就是添加的下一行的佈局屬性的唯一的事:

「機器人:背景=「@ drawable/image」

並記住你需要把你的背景圖像放入可繪製的文件夾中。

<?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:background="@drawable/image" > 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingBottom="10dip" 
     android:text="For parent" 
     android:id="@+id/parent" 
     android:layout_gravity="center_horizontal" /> 

    <Button 
     android:paddingBottom="10dip" 
     android:text="For parent" 
     android:id="@+id/children" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_centerInParent="true"/>  
</LinearLayout> 
0

將圖像放入drawable文件夾中。然後將其作爲背景添加到您的LinearLayout,屬性爲android:background="@drawable/your_image"。您可以將不同的圖像大小放入適當的可繪製文件夾中,例如drawable-hdpi,drawable-xhdpi等。您可以閱讀有關支持不同屏幕here的信息。 對於簡單的顏色和漸變,您可以使用Shape Drawable,對於真實圖像,我建議您將圖像視圖添加到佈局中(RelativeLayout),將圖像設置爲ImageView的源代碼並設置合適的scaleType(f.e.centerCrop)。

佈局層次:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/imageView" 
     android:src="@drawable/your_drawable" 
     android:scaleType="centerCrop"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:id="@+id/button"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:id="@+id/button2" 
     android:layout_below="@+id/button"/> 
</RelativeLayout>