我一直在网上寻找最有效和最干净的方式,以便在Android中以编程方式向视图添加元素,但我从来没有找到适合我想做的事情的方法。尽管我想使用推荐的工作方式(在XML文件中定义布局),但我将在下一部分中解释一些限制。
这就是我想要实现的目标。我有一个显示默认视图的活动(已在覆盖的onCreate方法中调用setContentView)。
我需要能够显示几个"弹出"风格消息,根据我的阅读,有两种方法,弹出窗口和 DialogFragments 。后者似乎更适合我,因为我在DialogFragment本身有一些特定的自定义(这是一个与扩展DialogFragment的活动分开的类)。
现在,我在XML文档中为对话框片段定义了一个简单的线性布局,但问题是,我需要向其中添加微调器,而且我从来不知道需要添加多少(因此,我可以& #39;在XML文件中手动定义微调器。)
我已经读过一些关于通货膨胀的内容,但从我的理解来看,它只是实例化了视图,因此它变得可见"可见"和"可操纵"在运行时但我的代码似乎确实被考虑在内。 (调用show方法时,视图不显示任何微调器。)
活动类:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbSwipeFM = getSupportFragmentManager();
drawView = new DrawingView(this,dbUtils);
[...]
setContentView(drawView);
drawView.requestFocus();
}
public void showColumnSelectionDialogFragment(ArrayList<Shape> shapesColToShow){
new ColumnSelectionDialog().show(dbSwipeFM, "fragment_edit_name");
}
从另一个类调用showColumnSelectionDialogFragment方法。
ColumnSelectionDialog类[编辑最后版本]:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//View view = inflater.inflate(R.layout.fragment_edit_name, container);
View view = new LinearLayout(getActivity());
DBUtils dbManager = DBUtils.get();
//to count pairs
int pairCount = 0;
LinearLayout horizontalLayoutForTables = new LinearLayout(view.getContext()),
horizontalLayoutForCols = new LinearLayout(view.getContext());
//set layout to be horizontal
horizontalLayoutForTables.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayoutForTables.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
horizontalLayoutForCols.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayoutForCols.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
ArrayList<Shape> dbManagerCurrentlySelectedTables = dbManager.getCurrentRequestSelectedTables();
ArrayList<String> allAvailableTables = new ArrayList<String>();
//((LinearLayout) view).addView(new TextView(view.getContext()));
//get the names of all table elements
for(int k =0; k < dbManager.getCurrentRequestSelectedTables().size(); k++){
allAvailableTables.add(dbManager.getCurrentRequestSelectedTables().get(k).getDbElement().getElementName());
}
for(int i = 0; i < dbManager.getCurrentRequestSelectedTables().size(); i++){
if(pairCount >= 1){
System.out.println("Adding layouts to view");
((LinearLayout) view).addView(horizontalLayoutForTables);
((LinearLayout) view).addView(horizontalLayoutForCols);
horizontalLayoutForTables = new LinearLayout(view.getContext());
horizontalLayoutForCols = new LinearLayout(view.getContext());
pairCount = 0;
}
//create a new spinner containing all the tables
Spinner tableSpinner = new Spinner(view.getContext());
tableSpinner.setAdapter(new SelectionDialogSpinnerAdapter(allAvailableTables));
//create a corresponding spinner for the columns of the current table only.
Spinner tableColumnSpinner = new Spinner(view.getContext());
ArrayList<String> columnNamesOnly = new ArrayList<String>();//get the column names only for the spinner
for(int j = 0; j < dbManagerCurrentlySelectedTables.get(i).getDbElement().getColumns().size(); j++){
columnNamesOnly.add(dbManagerCurrentlySelectedTables.get(i).getDbElement().getColumns().get(j).getColumnName());
}
tableColumnSpinner.setAdapter(new SelectionDialogSpinnerAdapter(columnNamesOnly));
//add to layout
System.out.println("Adding spinners to layout");
horizontalLayoutForTables.addView(tableSpinner);
horizontalLayoutForCols.addView(tableColumnSpinner);
pairCount++;
}
//view = inflater.inflate(R.layout.fragment_edit_name, container);
getDialog().setTitle(R.string.joinSelectTitle);
//view.invalidate();
this.view = view;
return view;
}
此方法:getCurrentRequestSelectedTables()。size();将根据最终用户的互动返回不同大小的结果。
视图现在只显示其标题。所以我在想,为这个视图创建一个全新的活动,然后用我当前活动的意图调用它是否更好?或者有一种方法可以动态创建视图(如何从活动传递上下文然后实例化它?)并以编程方式向其添加微调器。
谢谢。 [编辑]: 注意:还必须以编程方式添加在DialogFragment中创建的线性布局。 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/joinSelectMod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
</LinearLayout>
答案 0 :(得分:0)
将onCreateView更改为:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_id, container);
LinearLayout viewLayout = (LinearLayout) view.findViewById(R.layout.fragment_edit_name);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
DBUtils dbManager = DBUtils.get();
ArrayList<Shape> dbManagerCurrentlySelectedTables = dbManager.getCurrentRequestSelectedTables();
ArrayList<String> allAvailableTables = new ArrayList<String>();
//get the names of all table elements
for(int k =0; k < dbManager.getCurrentRequestSelectedTables().size(); k++){
allAvailableTables.add(dbManager.getCurrentRequestSelectedTables().get(k).getDbElement().getElementName());
}
for(int i = 0; i < dbManager.getCurrentRequestSelectedTables().size(); i++){
if(i % 2 == 0){
LinearLayout horizontalLayoutForTables = new LinearLayout(view.getContext()),
horizontalLayoutForCols = new LinearLayout(view.getContext());
//set layout to be horizontal
horizontalLayoutForTables.setOrientantion(LinearLayout.HORIZONTAL);
horizontalLayoutForCols.setOrientantion(LinearLayout.HORIZONTAL);
viewLayout.addView(horizontalLayoutForTables);
viewLayout.addView(horizontalLayoutForCols);
pairCount = 0;
}
//create a new spinner containing all the tables
Spinner tableSpinner = new Spinner(view.getContext());
tableSpinner.setAdapter(new SelectionDialogSpinnerAdapter(allAvailableTables));
//create a corresponding spinner for the columns of the current table only.
Spinner tableColumnSpinner = new Spinner(view.getContext());
ArrayList<String> columnNamesOnly = new ArrayList<String>();//get the column names only for the spinner
for(int j = 0; j < dbManagerCurrentlySelectedTables.get(i).getDbElement().getColumns().size(); j++){
columnNamesOnly.add(dbManagerCurrentlySelectedTables.get(i).getDbElement().getColumns().get(j).getColumnName());
}
tableColumnSpinner.setAdapter(new SelectionDialogSpinnerAdapter(columnNamesOnly));
//add to layout
horizontalLayoutForTables.addView(tableSpinner, llp);
horizontalLayoutForCols.addView(tableColumnSpinner, llp);
}
getDialog().setTitle(R.string.joinSelectTitle);
return view;
}
更新:包含用于在放入新的LinearLayout
时设置视图参数的代码