2015-10-13 145 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>