所以我有一个带有2个空ListView和3个按钮的活动。 “Hinzufügen”按钮(在屏幕截图中看到)应该将片段加载到Framelayout(用作容器)以覆盖活动中的所有组件(ListView和Buttons)。但是我不知道它为什么会落后于它们(不能单击片段的按钮,因为它位于一个空的ListView后面)。
没有片段的截图开始: https://abload.de/img/dqwdwqemuhi.jpeg
Fragment的截图开始: https://abload.de/img/fewqfewp7u0b.jpeg
如果我拿到
的容器View view = findViewById(R.id.container);
然后使用
view.bringtToFront();
活动中的按钮仍然显示,但ListView位于后台(现在可以单击片段中的按钮)。
如何从后台的活动中获取按钮,以便片段覆盖整个活动及其组件?
活动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>
活动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();
}
});
}
}
片段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:
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;
}
答案 0 :(得分:1)
将addCardsContainer
移至Activity
XML(ConstraintLayout
的最后一个孩子)的底部。绘图始终按照在充气布局文件中定位的顺序。 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>
由于边距,您仍然可以在下面的图层中看到按钮的片段。您可以将layout_margin
替换为padding
并为封面设置一些背景
android:padding="20dp"
android:background="#000000"