2017-06-06 29 views
0

所以我有一個活動,有2個空的ListView和3個按鈕。 「Hinzufügen」按鈕(見屏幕截圖)應該將一個片段加載到一個Framelayout(用作容器)以放置活動中的所有組件(ListView和Buttons)。但我不知道它爲什麼落後於它們(無法單擊片段的按鈕,因爲它位於空的ListView後面)。Android - 片段背後來自Activity的按鈕

截圖無片段開始: 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(); 
     } 
    }); 
} 

} 

片段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; 
} 

回答

0

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,併爲蓋

一些背景
 android:padding="20dp" 
     android:background="#000000" 
+0

已經嘗試過把它在底部,但仍然無法正常工作。 – ultimate2106

+0

它必須......它明顯是繪圖順序的問題。你能用這個'addCardsContainer'' FrameLayout'移動到底部嗎? – snachmsm

+0

https://puu.sh/wf8pS/5f816e267c.png(代碼截圖)... https://puu.sh/wf8Mz/0747fe7ab9.png(帶碎片的應用截圖)。沒有什麼變化,你可以看到:/ – ultimate2106