我正在创建一个基本的 android 应用程序来学习。下面我有在创建时创建一些 textViews 的主要活动。我试图通过将一大块代码放在另一个名为“CreateCategories.java”的文件中来清理我的项目,然后调用一个函数、类或运行该代码的东西。我该怎么做呢?下面是我目前的程序
package com.company.practice;
import ...
.
.
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout categoryLayout = findViewById(R.id.categoryContainer);
ArrayList<Category> categories = new ArrayList<Category>();
categories.add(new Category("rent", "0.35", "0.00"));
categories.add(new Category("loan", "0.1", "0.00"));
TextView[] catogoryTitleTextView = new TextView[categories.size()];
TextView[] catogorypercentageTextView = new TextView[categories.size()];
TextView[] catogoryAmountTextView = new TextView[categories.size()];
for(int i =0; i < categories.size(); i++){
//initialize textviews
TextView title = new TextView(this);
TextView percentage = new TextView(this);
TextView amount = new TextView(this);
//set text views text, id, and textsize
title.setText(categories.get(i).title);
//title.setTextSize(getResources().getDimension(R.dimen.textsize));
title.setId(i + 100);
percentage.setText(categories.get(i).percent);
//percentage.setTextSize(getResources().getDimension(R.dimen.textsize));
percentage.setId(i + 200);
amount.setText(categories.get(i).amount);
//amount.setTextSize(getResources().getDimension(R.dimen.textsize));
amount.setId(i + 300);
//set params for title textview
RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
titleParams.addRule(RelativeLayout.ALIGN_END, R.id.salaryCategoryTextVeiw);
titleParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
titleParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
if(i==0){
//set params for title textview if it the first one. it sets below the textveiw catagory, and has more margin
titleParams.addRule(RelativeLayout.BELOW, R.id.salaryCategoryTextVeiw);
titleParams.topMargin = 27;
} else {
//this will look up the id of teh last category text view
titleParams.addRule(RelativeLayout.BELOW, catogoryTitleTextView[i-1].getId());
titleParams.topMargin = 15;
}
title.setLayoutParams(titleParams);
//set params for percentage textview
RelativeLayout.LayoutParams PercentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
PercentParams.addRule(RelativeLayout.ALIGN_START, R.id.salaryPercentTextVeiw);
PercentParams.addRule(RelativeLayout.ALIGN_TOP, title.getId());
PercentParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
PercentParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
percentage.setLayoutParams(PercentParams);
//set params for amount textview
RelativeLayout.LayoutParams AmountParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
AmountParams.addRule(RelativeLayout.ALIGN_START, R.id.salaryAmountTextVeiw);
AmountParams.addRule(RelativeLayout.ALIGN_TOP, percentage.getId());
AmountParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
AmountParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
amount.setLayoutParams(AmountParams);
//add text views to layout
categoryLayout.addView(title);
categoryLayout.addView(percentage);
categoryLayout.addView(amount);
//save the views within the arrays
catogoryTitleTextView[i] = title;
catogorypercentageTextView[i] = percentage;
catogoryAmountTextView[i] = amount;
}
}
}
我希望它看起来像这样:MainActivty.java
package com.company.practice;
import ...
.
.
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
创建类别.java
Function CreateCategories() {
RelativeLayout categoryLayout = findViewById(R.id.categoryContainer);
ArrayList<Category> categories = new ArrayList<Category>();
categories.add(new Category("rent", "0.35", "0.00"));
categories.add(new Category("loan", "0.1", "0.00"));
TextView[] catogoryTitleTextView = new TextView[categories.size()];
TextView[] catogorypercentageTextView = new TextView[categories.size()];
TextView[] catogoryAmountTextView = new TextView[categories.size()];
for(int i =0; i < categories.size(); i++){
//initialize textviews
TextView title = new TextView(this);
TextView percentage = new TextView(this);
TextView amount = new TextView(this);
//set text views text, id, and textsize
title.setText(categories.get(i).title);
//title.setTextSize(getResources().getDimension(R.dimen.textsize));
title.setId(i + 100);
percentage.setText(categories.get(i).percent);
//percentage.setTextSize(getResources().getDimension(R.dimen.textsize));
percentage.setId(i + 200);
amount.setText(categories.get(i).amount);
//amount.setTextSize(getResources().getDimension(R.dimen.textsize));
amount.setId(i + 300);
//set params for title textview
RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
titleParams.addRule(RelativeLayout.ALIGN_END, R.id.salaryCategoryTextVeiw);
titleParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
titleParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
if(i==0){
//set params for title textview if it the first one. it sets below the textveiw catagory, and has more margin
titleParams.addRule(RelativeLayout.BELOW, R.id.salaryCategoryTextVeiw);
titleParams.topMargin = 27;
} else {
//this will look up the id of teh last category text view
titleParams.addRule(RelativeLayout.BELOW, catogoryTitleTextView[i-1].getId());
titleParams.topMargin = 15;
}
title.setLayoutParams(titleParams);
//set params for percentage textview
RelativeLayout.LayoutParams PercentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
PercentParams.addRule(RelativeLayout.ALIGN_START, R.id.salaryPercentTextVeiw);
PercentParams.addRule(RelativeLayout.ALIGN_TOP, title.getId());
PercentParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
PercentParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
percentage.setLayoutParams(PercentParams);
//set params for amount textview
RelativeLayout.LayoutParams AmountParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
AmountParams.addRule(RelativeLayout.ALIGN_START, R.id.salaryAmountTextVeiw);
AmountParams.addRule(RelativeLayout.ALIGN_TOP, percentage.getId());
AmountParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
AmountParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
amount.setLayoutParams(AmountParams);
//add text views to layout
categoryLayout.addView(title);
categoryLayout.addView(percentage);
categoryLayout.addView(amount);
//save the views within the arrays
catogoryTitleTextView[i] = title;
catogorypercentageTextView[i] = percentage;
catogoryAmountTextView[i] = amount;
}
任何有关如何做到这一点的有用建议将不胜感激。