在android中创建一个半透明的片段

时间:2015-01-23 07:01:28

标签: android android-styles translucency

我有一个覆盖整个屏幕的片段A和另一个位于50dp屏幕底部的片段B.片段B重叠片段AI的一些底部部分想要使片段B半透明所以片段的重叠部分看到了A.

我尝试使用Theme.Translucent,使用了framelayouts,使用了setAlpha()方法,但没有得到想要的结果。就像我们在android中有一个半透明的动作栏一样,我想让我的片段B半透明。

我试着参考这些链接...... link1: making the background translucent

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

LINK3: Making a android button’s background translucent,

一些代码可以帮助您理解..

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

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

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

任何建议或帮助伙伴......非常感谢。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以在帧布局中添加片段,它将重叠..

活动

    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();

    }

}

FragmentA布局

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

Fragmnet A Class

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();
                }
            });


    }

FragmentB类

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的正确方法