2015-03-19 102 views
0

我想創建一個屏幕,將有一個3按鈕的片段,並在它下面的文本框架,但我沒有看到任何東西,當我運行應用程序。 我是Android新手,所以我假設我缺少一些基本的東西。 我錯過了什麼?片段不會顯示在活動

編輯:謝謝你的所有答案。問題最終是因爲按鈕的大小不可見。 活動確實加載了片段,而無需調用片段管理器。

哦,學習一門新的框架:)

感謝您的樂趣!

activity_main.xml中

<LinearLayout 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:orientation="vertical"> 

<fragment 
    android:id="@+id/buttons" 
    android:layout_width="match_parent" 
    android:layout_height="400dp" 
    class="com.thenoisemargins.eyaldagiavdor.thisdaybefore.ButtonsFragment" /> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="100dp"> 

    <TextView 
     android:layout_width="48dp" 
     android:layout_height="45dp" 
     android:id="@+id/historicalQuoteTextView" 
     android:layout_gravity="center_vertical" 
     android:textAlignment="center" 
     android:textColor="#FFFFFFFF" 
     android:text="Quote" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</FrameLayout> 

buttons_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<at.markushi.ui.CircleButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:text="@string/this_day_before" 
    android:id="@+id/thisDayBeforeButton" 
    app:cb_color="#99CC00" 
    app:cb_pressedRingWidth="8dip" /> 

<at.markushi.ui.CircleButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:text="@string/choose_date" 
    android:id="@+id/ChooseDateButton" 
    app:cb_color="#99CC00" 
    app:cb_pressedRingWidth="8dip" /> 

<at.markushi.ui.CircleButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:text="@string/random_date" 
    android:id="@+id/RandomDateButton" 
    app:cb_color="#99CC00" 
    app:cb_pressedRingWidth="8dip" /> 
</LinearLayout> 

MainActivity.java

public class MainActivity extends ActionBarActivity { 

    private ButtonsFragment mFragment; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     /* 
     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     mFragment = new ButtonsFragment(); 
     fragmentTransaction.add(R.id.buttons, mFragment); 
     fragmentTransaction.commit(); 
       */ 

     mFragment = (ButtonsFragment) getFragmentManager() 
       .findFragmentById(R.id.buttons); 


    } 


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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

ButtonsFragment.java

public class ButtonsFragment extends Fragment { 

private at.markushi.ui.CircleButton mThisDayBeforeButton; 
private at.markushi.ui.CircleButton mChooseDateButton; 
private at.markushi.ui.CircleButton mRandomDateButton; 


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

    View rootView = inflater.inflate(R.layout.buttons_fragment, container, false); 



    return rootView; 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    mThisDayBeforeButton = (at.markushi.ui.CircleButton) getActivity().findViewById(R.id.thisDayBeforeButton); 
    mChooseDateButton = (at.markushi.ui.CircleButton) getActivity().findViewById(R.id.ChooseDateButton); 
    mRandomDateButton = (at.markushi.ui.CircleButton) getActivity().findViewById(R.id.RandomDateButton); 

} 

}上給出部分

回答

2

刪除評論。您必須使用fragmentTransaction將片段添加到fragmentManager中才能查看片段。

mFragment = (ButtonsFragment) getFragmentManager() 
       .findFragmentById(R.id.buttons); 

      FragmentManager fragmentManager = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      mFragment = new ButtonsFragment(); 
      fragmentTransaction.add(R.id.buttons, mFragment); 
      fragmentTransaction.commit(); 
+0

嘗試過它,仍然無法正常工作。 – 2015-03-19 17:55:32

0

試試這個,如果工作

FragmentManager fragmentManager = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      mFragment = new ButtonsFragment(); 
      fragmentTransaction.add(R.id.buttons, mFragment); 
      fragmentTransaction.commit(); 

上面的代碼替換這一點,並嘗試可能工作。

FragmentManager fragmentManager = getFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.buttons, fragment).commit(); 
0

如果你想直接在XML則顯示片段,

<fragment 
android:id="@+id/buttons" 
android:layout_width="match_parent" 
android:layout_height="400dp" 
class="com.thenoisemargins.eyaldagiavdor.thisdaybefore.ButtonsFragment" /> 

或者,如果你想以編程方式顯示,您需要使用

getSupportFragmentManager()

因爲您使用的是ActionbarActivity,如: add f ramelayou與id'框架'(示例)在佈局,然後將此代碼添加到您的活動

FrameLayout f = (FrameLayout) findViewById(R.id.fram); 
    getSupportFragmentManager().beginTransaction().replace(R.id.fram, new ButtonsFragment()).commit(); 

希望這有助於!

+0

同時檢查您是否正在擴展android.support.v4.app.Fragment片段類。 – Harry 2015-03-20 06:10:19