Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 其他按钮下的背景图像_Android_Layout - Fatal编程技术网

Android 其他按钮下的背景图像

Android 其他按钮下的背景图像,android,layout,Android,Layout,我是android新手,我正在尝试制作一个带有2个按钮的主活动屏幕,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_heigh

我是android新手,我正在尝试制作一个带有2个按钮的主活动屏幕,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个按钮将在它的顶部。如何存档?背景图像的大小应该是多少


谢谢您

它可以是如下所示的内容(imageview被拉伸到整个视图并向后设置,因此它将类似于背景图像):


设置根视图组的背景属性

将此更改为

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

只需在布局属性中添加下一行:

“android:background=“@drawable/image”

请记住,您需要将背景图像放入drawable文件夹

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

将图像放入
drawable
文件夹。然后使用属性
android:background=“@drawable/your\u image”将其作为背景添加到
LinearLayout
。您可以将不同大小的图像放入适当的可绘制文件夹,如
可绘制hdpi
可绘制xhdpi
等。您可以阅读有关支持不同屏幕的信息。 对于您可以使用的简单颜色和渐变,对于真实图像,我建议您将图像视图添加到布局中(
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>

你应该看看Android开发者。他们解释说

位图文件是.png、.jpg或.gif文件。当您将这些文件保存在res/Drawable/目录中时,Android会为它们创建一个可绘制资源

因此,您应该导入图像并将其放在/res/drawable/文件夹中。记住不同的屏幕尺寸

您正在寻找的androidxml符号称为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受其父LayoutParams的影响,例如边距)。使用ImageView将为您提供更多处理图像的自由,例如,将其缩放类型设置为适合布局的中心

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