创建一个没有边框的布局,覆盖在背景层上。

12

我想创建类似于截图中的这个东西。它有一个主要的布局作为背景和覆盖在其上的布局。我不确定这是如何实现的。

我最好的猜测是使用对话框片段。这是做这样的事情的正确方式吗?

enter image description here

我尝试使用对话框片段,但没有得到我想要的结果,这是我能够接近的程度。

enter image description here

这是我的布局:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.chatt.fragments.AddMember">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_highlight_off_white_36dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"/>

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/ivBackground"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:src="@drawable/world"
            android:scaleType="centerCrop"
            android:alpha="0.4"/>

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/civProfilePhoto"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/user3"
            app:layout_anchor="@+id/ivBackground"
            app:layout_anchorGravity="bottom|center"/>

        <TextView
            android:id="@+id/tvMemberName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-light"
            android:text="Sarah"
            android:textSize="26sp"
            android:textStyle="bold"
            app:layout_anchor="@+id/civProfilePhoto"
            app:layout_anchorGravity="bottom|center"
            android:layout_marginTop="40dp"/>

    </android.support.design.widget.CoordinatorLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="@color/material_grey_300">

        <ImageButton
            android:id="@+id/ibLock"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="60dp"
            android:layout_marginStart="60dp"
            android:src="@drawable/ic_lock_black_36dp"/>

        <ImageButton
            android:id="@+id/ibAddMember"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="60dp"
            android:layout_marginEnd="60dp"
            android:src="@drawable/ic_person_add_black_36dp"/>
    </RelativeLayout>
</RelativeLayout>

我已经使用了协调布局(Coordinator Layout)将个人资料图片锚定在背景上。然而,这样做没问题。但是,名字会遮盖一些个人资料图片。我从Google图标包中下载的图标。按钮显示非常暗的背景色,并且应该是透明的。右上角的关闭图标也没有透明的背景。对话框覆盖整个屏幕。

我像这样膨胀我的DialogFragment:

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStyle(DialogFragment.STYLE_NO_FRAME, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
        return inflater.inflate(R.layout.fragment_add_member, container, false);
    }

非常感谢任何建议,

2个回答

11

这是一个使用DialogFragment重新创建您屏幕截图UI的布局示例:

the result

fragment_add_member.xml布局:

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

    <ImageView
        android:layout_width="250dp"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/world"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_margin="5dp"
        android:src="@drawable/ic_highlight_off_white_36dp"/>

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/civProfilePhoto"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="185dp"
        android:src="@drawable/profile"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="240dp"
        android:gravity="center_horizontal"
        android:text="Mattia"
        android:textSize="16sp"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="320dp"
        android:background="@color/material_grey_300">

        <ImageView
            android:id="@+id/ibLock"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|start"
            android:layout_marginLeft="50dp"
            android:layout_marginStart="50dp"
            android:src="@drawable/ic_lock_black_36dp"/>

        <ImageView
            android:id="@+id/ibAddMember"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|end"
            android:layout_marginEnd="50dp"
            android:layout_marginRight="50dp"
            android:src="@drawable/ic_person_add_black_36dp"/>

    </FrameLayout>
</FrameLayout>

DialogFragment是什么:

public class AddMemberDialogFragment extends DialogFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_add_member, container);
    }
}

谢谢,那太完美了。 - ant2009

9

你可以选择两种方法:

1. 使用DialogFragment,这将自动为你提供阴影效果。

public class OverlayDialogFragment extends DialogFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // This will give you shadow with borderless frame.
        setStyle(DialogFragment.STYLE_NO_FRAME, 0);
    }

    // your code. define layout xml, etc....

}

2. 使用叠加布局和背景可绘制对象来创建阴影

在根RelativeLayout中定义两个布局,并调整覆盖的布局的宽度和高度。然后应用9-patch背景可绘制对象。

<RelativeLayout
 android:id="@+id/layoutRoot"
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

    <!-- your base UI elements here -->

    <RelativeLayout
        android:id="@+id/overlayLayout"
        android:layout_width="600dp"
        android:layout_height="800dp"
        android:background="@drawable/img_bg_mask"
        android:orientation="vertical">
    </RelativeLayout>
</RelativeLayout>

img_bg_mask.png看起来像下面这样。下面的图片显示了一个圆角示例,但您可以制作一个矩形阴影的9-patch图像。

输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接