,所以我有一个活动,其中有2个空列表视图和3个按钮。"hinzufügen"按钮(在屏幕快照中看到(应将片段加载到framelayout(用作容器(中,以放置活动中的所有组件(ListView和按钮(。但是我不知道为什么它在所有这些后面都放置(无法单击片段的按钮,因为它在空名单后面(。
屏幕截图没有碎片开始:https://abload.de/img/dqwdwqemuhi.jpeg
带有片段的屏幕截图开始: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();
}
});
}
}
fragment 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>
碎片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;
}
将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>
由于边缘,您可能仍然在下面的图层中看到按钮的片段。您可以用padding
替换layout_margin
,并为Cover设置一些背景
android:padding="20dp"
android:background="#000000"