2014-04-02 112 views
0

我正在關注一個片段示例https://gist.github.com/daichan4649/2947119活動視圖重疊片段

我在按鈕單擊事件後動態添加片段,但片段與按鈕重疊。當我在此屏幕上按回鍵時,活動將關閉。

需要幫助才能顯示單擊按鈕後的片段,並且如果在添加片段後按下了後退,則只應關閉頂部片段。

這是我的測試代碼。 MainActivity.java

public class MainActivity extends FragmentActivity { 
private static Button mBtn; 
private static final String TAG_LIST = "list"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
if (savedInstanceState != null) { 
return; 
} 

mBtn = (Button) findViewById(R.id.clickme); 
mBtn.setOnClickListener(new View.OnClickListener() { 

@SuppressLint("NewApi") 
@Override 
public void onClick(View v) { 
// TODO Auto-generated method stub 
Fragment fragment = getFragmentManager().findFragmentByTag(TAG_LIST); 
      if (fragment == null) { 
       fragment = TestListFragment.newInstance(); 
       FragmentTransaction ft = getFragmentManager().beginTransaction(); 
       ft.add(android.R.id.content, fragment, TAG_LIST); 
       ft.commit(); 
      } 

     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

TestListFragment.java

public class TestListFragment extends ListFragment { 

public static TestListFragment newInstance() { 
    return new TestListFragment(); 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
    ViewGroup rootView = (ViewGroup) inflater.inflate(android.R.id.content, container, false);   
    return rootView; 
} 
@SuppressLint("NewApi") 
@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_column, R.id.text); 
    setListAdapter(adapter); 
    adapter.addAll(createDataList(10)); 
} 

private static List<String> createDataList(int counts) { 
    List<String> list = new ArrayList<String>(); 
    for (int i = 0; i < counts; i++) { 
     list.add("i=" + i); 
    } 
    return list; 
} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    //Toast.makeText(getActivity(), "List Item Clicked", Toast.LENGTH_SHORT).show(); 

    //On item click, launch the DialogFragment 
    TestDialogFragment moveFragment = new TestDialogFragment(); 
    moveFragment.show(getFragmentManager(), "Test List Fragment"); 
} 

TestDilogFragment.java

public class TestDialogFragment extends DialogFragment { 

private View testDialogFragment = null; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    //Fill the layout as per Your requirement 
    testDialogFragment = inflater.inflate(R.layout.activity_list_fragment, container); 
    return testDialogFragment; 
} 

} 

活性-main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/clickme" 
    android:text="ClickMe" /> 

</RelativeLayout> 

activity_list_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

</RelativeLayout> 

回答

0

更改activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="match_parent" 
    android:layout_height="match_parentt" > 

    <Button 
     android:id="@+id/clickme" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="Button" /> 

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_above="@id/clickme" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     > 
    </FrameLayout> 

</RelativeLayout> 

你有Button在底部和ViewGroup是在頂部FrameLayou噸。您將Fragment添加到容器。

而且在ManiActivtiy變化

ft.add(R.id.container, fragment, TAG_LIST);