简体   繁体   English

Android - Fragment 位于 Activity 的 Buttons 后面

[英]Android - Fragment is behind Buttons from Activity

So I have an activity with 2 empty ListView's and 3 Buttons.所以我有一个带有 2 个空 ListView 和 3 个按钮的活动。 The "Hinzufügen" Button (seen in Screenshot) should load a Fragment into a Framelayout (used as Container) to lay over all components (ListView's and Buttons) from the activity. “Hinzufügen”按钮(在屏幕截图中看到)应该将 Fragment 加载到 Framelayout(用作容器)中,以覆盖活动中的所有组件(ListView 和按钮)。 But I don't know why it lays behind all of them (can't click the Button of the Fragment because it's behind an empty ListView).但我不知道为什么它位于所有这些后面(无法单击 Fragment 的按钮,因为它位于一个空的 ListView 后面)。

Screenshot without Fragment started: https://abload.de/img/dqwdwqemuhi.jpeg没有 Fragment 的截图开始: https : //abload.de/img/dqwdwqemuhi.jpeg

Screenshot with Fragment started: https://abload.de/img/fewqfewp7u0b.jpeg Fragment 启动截图: https : //abload.de/img/fewqfewp7u0b.jpeg

If I get the container with如果我得到容器

View view = findViewById(R.id.container);

and then use然后使用

view.bringtToFront();

the Buttons from the activity are still shown but the ListView's are in the background (can click the button from the fragment now).活动中的按钮仍然显示,但 ListView 位于后台(现在可以单击片段中的按钮)。

How do I get the buttons from the activity in the background so that the fragment lays over the whole activity and its components?如何从后台的活动中获取按钮,以便片段覆盖整个活动及其组件?

Activity XML:活动 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="superhelden.com.superheldenuebersichtsapp.Activities.DeckOverview">

<FrameLayout
    android:id="@+id/addCardsContainer"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

</FrameLayout>

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline3"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.65"
    tools:layout_editor_absoluteY="332dp"
    tools:layout_editor_absoluteX="0dp" />

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline4"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.4"
    tools:layout_editor_absoluteY="0dp"
    tools:layout_editor_absoluteX="154dp" />

<Button
    android:id="@+id/addCards"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:text="@string/addCards"
    app:layout_constraintRight_toLeftOf="@+id/guideline4"
    app:layout_constraintTop_toTopOf="@+id/guideline3"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<Button
    android:id="@+id/removeCards"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="@string/removeCards"
    app:layout_constraintBottom_toTopOf="@+id/guideline5"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/guideline4"
    app:layout_constraintTop_toBottomOf="@+id/addCards"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp" />

<Button
    android:id="@+id/endGame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="@string/endGame"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/guideline4"
    app:layout_constraintTop_toTopOf="@+id/guideline5"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp" />

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline5"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.88"
    tools:layout_editor_absoluteY="450dp"
    tools:layout_editor_absoluteX="0dp" />

<ListView
    android:id="@+id/amountOfTypesList"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintLeft_toLeftOf="@+id/guideline4"
    app:layout_constraintTop_toTopOf="@+id/guideline3"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp" />

<ListView
    android:id="@+id/deckList"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:divider="#000"
    android:dividerHeight="0.5dp"
    android:scrollbars="horizontal"
    app:layout_constraintBottom_toTopOf="@+id/guideline3"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

</android.support.constraint.ConstraintLayout>

Activity Java:活动Java:

public class DeckOverview extends AppCompatActivity {

FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
Button addCardsButton;
Button removeCardsButton;
Button endGameButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_deck_overview);
    addCardsButton = (Button) findViewById(R.id.addCards);
    addCardsButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            fragmentManager = getFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.addCardsContainer, new addCards());
            fragmentTransaction.addToBackStack("addCards");
            fragmentTransaction.commit();
        }
    });
    removeCardsButton = (Button) findViewById(R.id.removeCards);
    removeCardsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //TODO: Karten aus Tabelle in der DB entfernen
        }
    });
    endGameButton = (Button) findViewById(R.id.endGame);
    endGameButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), EndScreen.class);
            startActivity(intent);
            finish();
        }
    });
}

}

Fragment XML:片段 XML:

<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"
android:background="@color/colorAccent"
tools:context="superhelden.com.superheldenuebersichtsapp.Fragments.addCards">

<ListView
    android:id="@+id/allCardsList"
    android:layout_width="0dp"
    android:layout_height="0dp"

    app:layout_constraintBottom_toTopOf="@+id/guideline7"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/guideline6"
    app:layout_constraintTop_toTopOf="@+id/guideline10"

    style="@style/Margin_And_Divider_Style"/>

<ListView
    android:id="@+id/selectedCardsList"
    android:layout_width="0dp"
    android:layout_height="0dp"

    app:layout_constraintBottom_toTopOf="@+id/guideline7"
    app:layout_constraintLeft_toLeftOf="@+id/guideline6"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline10"

    style="@style/Margin_And_Divider_Style"/>

<Button
    android:id="@+id/doneButtonAddCards"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="@string/done"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline7"
    app:layout_constraintVertical_bias="0.571" />

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline6"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5"
    tools:layout_editor_absoluteY="0dp"
    tools:layout_editor_absoluteX="180dp" />

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline7"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.86"
    tools:layout_editor_absoluteY="439dp"
    tools:layout_editor_absoluteX="0dp" />

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline10"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.1"
    tools:layout_editor_absoluteY="51dp"
    tools:layout_editor_absoluteX="0dp" />

<SearchView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:id="@+id/searchView"

    app:layout_constraintBottom_toTopOf="@+id/guideline10"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

Fragment Java:片段Java:

List<Card> allCardsList;
ListView allCardsListView;

View addCards;

public addCards() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    allCardsList = DbClass.getInstance(getActivity()).getAllCards();
    allCardsListView = (ListView) getActivity().findViewById(R.id.allCardsList);

    Toast.makeText(getActivity(), allCardsList.get(0).getName(), Toast.LENGTH_SHORT).show();
    addCards = inflater.inflate(R.layout.fragment_add_cards, container, false);

    return addCards;
}

move addCardsContainer to the bottom of your Activity XML (last child of ConstraintLayout ).addCardsContainer移动到Activity XML 的底部( ConstraintLayout最后一个孩子)。 Drawing is always in order of positioning in inflated layout file.绘图始终按在膨胀布局文件中的定位顺序排列。 bringToFront is not for this case bringToFront不适用于这种情况

    ...
    <FrameLayout
        android:id="@+id/addCardsContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

because of margin you may still see a fragment of your buttons in layer below.由于边距,您可能仍会在下面的图层中看到按钮的片段。 you can replace layout_margin with padding and set some background for cover您可以用padding替换layout_margin并为封面设置一些背景

        android:padding="20dp"
        android:background="#000000"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM