0

我一直在网上寻找最有效和最干净的方式在 Android 中以编程方式向一般视图中添加元素的最有效和最干净的方法,但我从来没有完全找到我想要做的事情的方法。尽管我想使用推荐的工作方式(在 XML 文件中定义布局),但仍有一些限制,我将在下一部分中解释。

这就是我想要实现的目标。我有一个显示默认视图的活动(已在覆盖的 onCreate 方法中调用了 setContentView)。

我需要能够显示几个“弹出”样式的消息,根据我的阅读,有两种方法可以做到这一点,PopupsDialogFragments。后者似乎更适合我,因为我在 DialogFragment 本身(这是一个与我的扩展 DialogFragment 的活动分开的类)进行了一些特定的定制。

现在,我在 XML 文档中为对话框片段定义了一个简单的线性布局,但问题是,我需要向其中添加微调器,而且我事先不知道需要添加多少(因此,我无法定义在 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 类 [EDIT-last version]:

@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>
4

1 回答 1

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 时包含设置视图参数的代码

于 2013-07-24T01:37:48.953 回答