Android将活动分为两部分

时间:2017-09-11 20:27:16

标签: android android-fragments android-activity effect divide

目前,我正在为Android平板电脑开发应用程序。我搜索一种方法或方法将屏幕分成两部分并使子视图可见。我不知道我在哪里发现了这种效果(iOS或Android应用程序)。

为了说明,我附上了两张照片:

enter image description here

点击View X后,子视图应正好显示在下方(屏幕的其余部分将向下移动):

enter image description here

有谁知道这种效果? 问候!

2 个答案:

答案 0 :(得分:1)

在android studio中,你可以将一个片段膨胀到你的活动中。

以下是示例:

Main2Activity.java中的

public class Main2Activity extends AppCompatActivity {

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

        final FrameLayout frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
        Button btn = (Button) findViewById(R.id.button3);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PlusOneFragment myf = new PlusOneFragment();
                frameLayout.setVisibility(View.VISIBLE);
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.add(R.id.frameLayout, myf);
                transaction.commit();

            }
        });
    }
}

在activity_main2.xml

<?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"
    tools:context="com.testing.testinglibraryapps.Main2Activity">

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginStart="8dp" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginEnd="8dp" />

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="0dp"
        android:visibility="gone"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button3">

    </FrameLayout>

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/frameLayout" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="0dp"
        app:layout_constraintTop_toBottomOf="@+id/frameLayout" />
</android.support.constraint.ConstraintLayout>

看一看,我把一个framelayout可见性的技巧放在它们下面。

对于我的例子中的PlusOneFragment,你可以用你自己的片段来改变它

答案 1 :(得分:0)

你可以通过很多方式实现这一目标。我能想到的最简单的方法就是拥有一个可以直接显示/隐藏的视图。

布局:

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


    <LinearLayout   <!-- this is the top layout -->
        android:id:@+id/myTopLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation=horizontal>

        .... YOUR LAYOUT VIEWS ....

    </LinearLayout>

    <LinearLayout   <!-- this is layout you will show/hide -->
        android:id:@+id/myShowHideLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation=horizontal
        android:visibility="gone">

        .... YOUR LAYOUT VIEWS ....

    </LinearLayout>

    <LinearLayout   <!-- this is the bottom layout -->
        android:id:@+id/myBottomLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation=horizontal>

        .... YOUR LAYOUT VIEWS ....

    </LinearLayout>

</LinearLayout>

现在,在您的活动中,只需显示或隐藏您想要的中间布局:

LinearLayout myShowHideLayout;

... onCreate(...){

    myShowHideLayout = (LinearLayout)findViewById(R.id. myShowHideLayout);
    myShowHideLayout.setVisibility(View.VISIBLE);
}
相关问题