在框架布局中显示/隐藏onTouch的布局

时间:2018-06-25 08:44:24

标签: android android-framelayout

我有完整的图像片段。在framelayout中有一个RelativeLayout和LinearLayout。 LinearLayout包含viewpager。

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearLayoutViewpager">


    <com.bogdwellers.pinchtozoom.view.ImageViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>

<RelativeLayout
    android:id="@+id/relativelayoutforbuttons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/save_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />


</RelativeLayout>

我想要当用户第一次打开它时,它应该仅显示图像(Viewpager布局),如果用户单击它,则显示其他选项,例如-share and save(RelativeLayout)。 我们可以在facebook和whatsapp中找到这种类型的功能。

2 个答案:

答案 0 :(得分:1)

请尝试使用此类型的代码以保持友善..

private void share(){
    relativeLayout.setVisibility(View.VISIBLE);
    linearLayoutViewpager.setVisibility(View.GONE);
}


 linearLayoutViewpager=findViewById(R.id.linearLayoutViewpager);
    linearLayoutViewpager.setVisibility(View.VISIBLE);
    relativeLayout=findViewById(R.id.relativelayoutforbuttons);
    relativeLayout.setVisibility(View.GONE);

它只是示例代码,但可以根据您的需要进行管理。

     final boolean flag=true;
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (flag){
                relativeLayout.setVisibility(View.VISIBLE);
                flag=false;
            }
            else{
                relativeLayout.setVisibility(View.GONE);
            }
        }
    });

答案 1 :(得分:1)

您可以使用linearLayout或imageviewpager单击上的 setVisibility。(..)方法来简单地隐藏或取消隐藏relativeLayout的实例。

布局:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/linearLayoutViewpager">


        <ImageViewPager
            android:id="@+id/viewpager"
            android:onClick="onPagerClick"
            android:layout_width="200dp"
            android:layout_height="200dp" />


    </LinearLayout>

    <RelativeLayout
        android:id="@+id/relative_layout"
        android:visibility="gone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/save_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onSave"
        android:text="Save" />


</RelativeLayout>

</FrameLayout>

活动:

   public class TestActivity extends AppCompatActivity {

    private boolean isButtonShown = false;
    ImageViewPager viewPager;
    RelativeLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        viewPager = findViewById(R.id.viewpager);

        layout = findViewById(R.id.relative_layout);




    }


    public void onPagerClick(View view) {

        if (isButtonShown) {
            isButtonShown = false;
            layout.setVisibility(View.GONE);
        } else {
            isButtonShown = true;
            layout.setVisibility(View.VISIBLE);
        }

    }
}
相关问题