2016-04-24 70 views
1

我只是盯着Android應用程序開發,並有一個關於在MainActivity的onCreate()膨脹佈局的問題。也許我不是在問正確的問題。但基本上我有2個選項卡。我有一次單擊應該填充第二選項卡上表中的第一個選項卡上的計算按鈕Android的 - 如何在onCreate()中膨脹xml

public class MainActivity extends AppCompatActivity { 

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

     new MyTableFragment(); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
     tabLayout.addTab(tabLayout.newTab().setText("Details")); 
     tabLayout.addTab(tabLayout.newTab().setText("Discount")); 
     tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

     final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 
     final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount()); 
     viewPager.setAdapter(adapter); 
     viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 

     this.init(); 
    } 
    public void onButtonClick(View view) { 

     EditText text = (EditText) findViewById(R.id.amount); 
     _amount = Integer.parseInt(text.getText().toString()); 

     text = (EditText) findViewById(R.id.rate); 
     _rate = Integer.parseInt(text.getText().toString()) * 12; 

     EditText discount = (EditText) findViewById(R.id.discount); 
     discount.setText(Double.toString(_discount)); 

     this.init(); 
    } 

    public void init(){ 
     TableLayout tableLayout = (TableLayout) findViewById(R.id.table_main); 
     if(tableLayout == null) 
      System.out.println("Table Layout is NULL"); 
     else 
      System.out.println("Layout is not null"); 

     TableRow tbrow0 = new TableRow(this); 
     TextView tv0 = new TextView(this); 
     tv0.setText("Date"); 
     tv0.setTextColor(Color.WHITE); 
     tbrow0.addView(tv0); 
     TextView tv1 = new TextView(this); 
     tv1.setText(" Amount "); 
     tv1.setTextColor(Color.WHITE); 
     tbrow0.addView(tv1); 
     TextView tv2 = new TextView(this); 
     tv2.setText(" Discount "); 
     tv2.setTextColor(Color.WHITE); 
     tbrow0.addView(tv2); 

     for (int i = 0; i < _loanTerm; ++i) { 
      TableRow tbrow = new TableRow(this); 
      TextView t1v = new TextView(this); 
      t1v.setText("" + i); 
      t1v.setTextColor(Color.WHITE); 
      t1v.setGravity(Gravity.CENTER); 
      tbrow.addView(t1v); 
      TextView t2v = new TextView(this); 
      t2v.setText("Product " + i); 
      t2v.setTextColor(Color.WHITE); 
      t2v.setGravity(Gravity.CENTER); 
      tbrow.addView(t2v); 
      tableLayout.addView(tbrow); 
     } 
    } 
} 

public class MyTableFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.my_table, container, false); 
    } 
} 

public class PagerAdapter extends FragmentStatePagerAdapter { 
    private int _numOfTabs; 

    public PagerAdapter(FragmentManager fm, int NumOfTabs) { 
     super(fm); 
     this._numOfTabs = NumOfTabs; 
    } 

    @Override 
    public Fragment getItem(int position) { 

     switch (position) { 
      case 0: 
       TabFragment1 tab1 = new TabFragment1(); 
       return tab1; 
      case 1: 
       TabFragment2 tab2 = new TabFragment2(); 
       return tab2; 
      default: 
       return null; 
     } 
    } 

    @Override 
    public int getCount() { 
     return _numOfTabs; 
    } 

這裏是activity_main.xml

<RelativeLayout 
    android:id="@+id/main_layout" 
    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=".MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:background="?attr/colorPrimary" 
     android:elevation="6dp" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/toolbar" 
     android:background="?attr/colorPrimary" 
     android:elevation="6dp" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/tab_layout"/> 

</RelativeLayout> 

這裏是tab_fragment_1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/AMOUNT" 
    android:id="@+id/textView" 
    android:layout_alignBottom="@+id/Amount" 
    android:layout_toStartOf="@+id/calculateButton" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="number" 
    android:ems="10" 
    android:id="@+id/Amount" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentEnd="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/RATE" 
    android:id="@+id/textView2" 
    android:layout_marginTop="32dp" 
    android:layout_below="@+id/Amount" 
    android:layout_alignEnd="@+id/textView" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="number" 
    android:ems="10" 
    android:id="@+id/rate" 
    android:layout_alignBottom="@+id/textView2" 
    android:layout_alignStart="@+id/Amount" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/DISCOUNT" 
    android:id="@+id/textView4" 
    android:layout_centerVertical="true" 
    android:layout_toStartOf="@+id/calculateButton" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:id="@+id/discount" 
    android:editable="false" 
    android:inputType="none" 
    android:layout_alignBottom="@+id/textView4" 
    android:layout_alignStart="@+id/rate" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/CALCULATE" 
    android:id="@+id/calculateButton" 
    android:clickable="true" 
    android:enabled="true" 
    android:onClick="onButtonClick" 
    android:layout_above="@+id/payment" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="27dp" /> 

</RelativeLayout> 

這裏是tab_fragment_2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="Tab 2" 
     android:textAppearance="?android:attr/textAppearanceLarge"/> 

</RelativeLayout> 

這裏是我的my_table.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#3d455b" 
     android:layout_alignParentLeft="true" > 

     <HorizontalScrollView 
      android:id="@+id/hscrll1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

      <RelativeLayout 
       android:id="@+id/RelativeLayout1" 
       android:layout_width="fill_parent" 
       android:layout_gravity="center" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" > 

       <TableLayout 
        android:id="@+id/table_main" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true" > 
       </TableLayout> 
      </RelativeLayout> 
     </HorizontalScrollView> 
    </ScrollView> 

</LinearLayout> 

在我的init(),當我嘗試訪問

TableLayout tableLayout = (TableLayout) findViewById(R.id.table_main);

返回null

我缺少什麼?

感謝

+0

哪裏是'init'的代碼? –

+0

你在片段或活動中使用表格佈局嗎? –

+0

在'return inflater.inflate(R.layout.my_table,container,false);'處設置一個斷點。確保它被調用。 – CaptJak

回答

1

如果你想你充氣應先有活動主內的FrameLayout片段,然後更換爲選用的片段的FrameLayout。

公共類MainActivity延伸AppCompatActivity {

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //change content_frame for the id fiven to the frame layout inside activity_main 
    getFragmentManager().beginTransaction().add(R.id.content_frame, new MyTableFragment()).commit(); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
    tabLayout.addTab(tabLayout.newTab().setText("Loan Details")); 
    tabLayout.addTab(tabLayout.newTab().setText("Amortization")); 
    tabLayout.addTab(tabLayout.newTab().setText("Tab 3")); 
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 
    final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount()); 
    viewPager.setAdapter(adapter); 
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 

    this.init(); 
} 

}

公共類MyTableFragment延伸片段{ @覆蓋 公共視圖onCreateView(LayoutInflater充氣,ViewGroup中的容器中,捆綁savedInstanceState){ 返回充氣。 inflate(R.layout.my_table,container,false); } }

public class MyTableFragment extends Fragment { 
View view; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    view = inflater.inflate(R.layout.my_table, container, false); 
    //in here you find the table using the view 
    TabLayout tabLayout = (TabLayout)view.findViewById(R.id.tab_layout); 

    return view; 
} 

}

+0

謝謝! 我不清楚我需要做什麼這條線 'getFragmentManager()。beginTransaction()。add(R.id.content_frame,new MyTableFragment())commit();' 我有3在我的應用程序標籤。我想讓表格顯示在我的第二個標籤中。那麼我需要在tab_fragment_1.xml中添加一個新的框架佈局嗎? 這個想法是,我想要按tab1上的按鈕並填充tab2中的表格 我還沒有發佈我的tab_fragment_1.xml和tab_fragment_2.xml。如果需要的話,我可以更新我的OP。 – RBAustin

+0

如果您想要更具體的幫助,請顯示您的完整代碼並準確解釋您想要的內容。這裏是tabhost交換片段的鏈接。 https://thepseudocoder.wordpress.com/2011/10/13/android-tabs-viewpager-swipe-able-tabs-ftw/ –

+0

我剛剛更新了OP。幾乎所有的代碼都在那裏。道歉的長期職位... – RBAustin

0

你要消耗你的片段在活動第一

操作步驟如下:

首先在主活動佈局定義容器託管的片段並給它分配id,假設這個ID是「fragment_container」, 所以在該行之後:

setContentView(R.layout.activity_main); 

添加以下行:

getFragmentManager().beginTransaction().add(R.id.fragment_container, new MyTableFragment()).commit();