在安卓中制作半透明的片段



我有一个片段A覆盖了整个屏幕,另一个片段B位于50dp的屏幕底部。片段B重叠片段A的某些底部,我想使片段B半透明,以便看到片段A的重叠部分。

我尝试使用Theme.Semilucent,使用框架布局,使用setAlpha()方法,但没有得到想要的结果。就像我们在安卓中有一个半透明的操作栏一样,我也想让我的片段 B 半透明。

我尝试参考这些链接...链接 1 :使背景半透明

链接 2:https://zaman91.wordpress.com/2010/03/22/android-how-to-create-transparent-or-opeque-background/

链接3:使安卓按钮的背景半透明,

一些代码来帮助你理解..

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.examples"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@null"
    android:fitsSystemWindows="true" >
    <!-- Your normal content view -->
    <com.examples.views.SlidingUpPanelLayout
        android:id="@+id/sliding_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center|top" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/icn_actionbar_background"
                android:minHeight="?attr/actionBarSize" >
                <com.examples.views.CustomTextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:singleLine="true"
                    android:text="Toolbar Title"
                    android:textColor="@color/action_bar_text"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    app:font="@string/font_avenirMedium" />
            </android.support.v7.widget.Toolbar>
            <!-- Say this is Fragment A -->
            <fragment
                android:id="@+id/frag_fragmentA"
                android:name="com.examples.fragments.FragmentA"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center" />
            <!-- The rest of your content view -->
        </LinearLayout>
                      <!-- this is fragment B -->
        <fragment
            android:id="@+id/fragment B"
            android:name="com.examples.fragments.MyFragmentB"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center|top" />
    </com.examples.SlidingUpPanelLayout>
    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:fitsSystemWindows="true" >
        <!-- Your drawer content -->
        <include
            android:id="@+id/left_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            layout="@layout/drawer_layout" />
    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

50dp的偏移量在SlidingPanelLayout本身中给出。所以只有 50 dp 的片段 B 可见。

在所有这些链接中,主题.半透明被添加到活动主题中。但我想创建一个主题,只使该片段半透明。

任何建议或帮助伙计们..深表感谢。谢谢。

您可以在框架布局中添加片段,它将重叠。

活动

    public class MainBaseFragmentActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.main_fragment_container);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.main_container, new FragmentScreenA()).commit();
    }
}

片段布局

<?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:background="#ff00ddff"
     >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_screen_a"
        android:gravity="center_horizontal"
        android:text="A"
        android:textColor="#ffffff"
        android:textSize="100sp" />
    <Button
        android:id="@+id/btn_screen_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Go to Screen B" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" 
        android:text="Text From Fragment A" android:gravity="center" android:textSize="30sp"
        android:textColor="#ffffff" />
</RelativeLayout>

弗拉格姆内特A级

public class FragmentScreenA extends Fragment {
    private Button btn_screen_a;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView= inflater.inflate(R.layout.fragment_screen_a, container, false);
        return rootView;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
            btn_screen_a = (Button) rootView.findViewById(R.id.btn_screen_a);
            btn_screen_a.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().getSupportFragmentManager()
                            .beginTransaction()
                            .add(R.id.main_container, new FragmentScreenB())
                            .addToBackStack("FragmentScreenB").commit();
                }
            });

    }

片段B类

public class FragmentScreenB extends Fragment {
    private Button btn_screen_b;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_screen_b, container,
                false);
        return rootView;
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
            btn_screen_b = (Button) view.findViewById(R.id.btn_screen_b);
            btn_screen_b.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity()
                            .getSupportFragmentManager()
                            .beginTransaction()
                            .replace(R.id.main_container, new FragmentScreenC())
                            .addToBackStack("FragmentScreenB").commit();
                }
            });
    }
    private View rootView;

}

片段 B XMl 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:gravity="center"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" android:layout_weight="9"
        android:orientation="vertical" >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="B"
            android:textSize="100sp" />
        <Button
            android:id="@+id/btn_screen_b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go to Screen C" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"  android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>
</LinearLayout>

![在此输入图像描述][1]![在此输入图像描述][2]这是针对您提出的解决方案。建议您可以在同一屏幕中创建多个片段。这是正确的方式 AFAIK

最新更新