gpt4 book ai didi

Android - 如何在 onCreate( ) 中扩充 xml

转载 作者:行者123 更新时间:2023-11-30 01:22:58 26 4
gpt4 key购买 nike

我刚开始开发 Android 应用程序,对 MainActivity 的 onCreate() 中的扩充布局有疑问。可能是我没有问正确的问题。但基本上我有 2 个标签。我在第一个选项卡上有一个计算按钮,单击后应该会在第二个选项卡上填充一个表格

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);

它返回空

我错过了什么?

谢谢

最佳答案

如果你想膨胀一个 fragment ,你应该首先在 activity main 中有一个框架布局,然后替换所选 fragment 的框架布局。

公共(public)类 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();
}

公共(public)类 MyTableFragment 扩展 fragment { @覆盖 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 返回 inflater.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;
}

关于Android - 如何在 onCreate( ) 中扩充 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36824254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com