繁体   English   中英

如何在可绘制资源中创建此布局并将其作为活动背景

[英]How to create this layout in Drawable Resource and make it as background in activity

图片参考

我只想在顶部创建并显示示例图片作为活动的背景。 提前致谢

在你的 xml 上写:android:background=@drawable/imgname.extension

这必须在您的

有很多方法可以设计这种布局

首先将图像设置为这样的背景

android:background="@drawable/dummy"

其次创建自定义布局并用作这样的背景

laout_custom_bg.xml(添加布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight=".2"
    android:background="#fff"/>
    <View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight=".8"
        android:background="#2196F3"/>
</LinearLayout>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <include
        layout="@layout/layout_custom_bg"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:textSize="30sp"
            android:textStyle="bold"
            android:textColor="#FF9800"/>
    </LinearLayout>
</FrameLayout>

在此处输入图像描述

使用RelativeLayout,它的孩子可以有海拔(z轴上的高度)。 因此,您将背景图片与父级匹配(宽度和高度),并且高度为0。然后所有其他视图的高度> 0。

编辑:您还可以使用约束布局,如:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/imageView"
        android:elevation="0dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@yourpicturesource" />

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

    ...other views



</android.support.constraint.ConstraintLayout>

有很多方法可以创建视图。

我在这里也使用了LinearLayout

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal"
   >

   <View
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight=1
     android:background="#FFFFFF"
   />

   <View
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight=3
     android:background="#0000FF"
   />
</LinearLayout

getWindow().setBackgroundDrawableResource(R.drawable.example);

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        getWindow().setBackgroundDrawableResource(R.drawable.example);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM