如何以编程方式在LinearLayout中相互添加两个视图?

时间:2018-06-18 11:28:15

标签: android android-linearlayout

我已经看到了一些关于如何使用XML在LinearLayout中相互添加两个视图的解释。如何以编程方式完成?

我有以下两种方法:

private void createListOfInstalledApplications() {
    installedApplicationNames = this.getApplicationNames();
    installedPackages = this.getPackageNames();

    for(int i = 0; i < installedApplicationNames.size(); i++){
        TextView scrollAppsView = new TextView(this);
        scrollAppsView.setId(i);
        scrollAppsView.setText(installedApplicationNames.get(i) + "\n ");
        layout.addView(scrollAppsView);
    }

}

此方法会将Android手机上已安装应用程序的名称添加到LinearLayout。在这些应用程序的右侧,我想有一个下拉菜单,可以选择一些设置。目前,以下方法将Spinners添加到LinearLayout

private void createDropDownMenuesForApplications() {
    List<String> vibPatternNames = new ArrayList<String>();
    VibrationPatternManager vibPatManager = VibrationPatternManager.getInstance();

    List<VibrationPattern> vibrationPatterns = vibPatManager.getAllAvailablePatterns();

    for(VibrationPattern vibrationPattern : vibrationPatterns){
        vibPatternNames.add(vibrationPattern.getName());
    }

    for(int i = 0; i < installedApplicationNames.size(); i++){
        Spinner dropDownMenu = new Spinner(this);
        dropDownMenu.setId(i);

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, vibPatternNames);
        dropDownMenu.setAdapter(dataAdapter);


        layout.addView(dropDownMenu);
    }
}

下拉菜单现在显示在应用程序名称下方。但是,我希望它们出现在右边。所以 &#34; ApplicationName1下拉菜单1&#34;

而不是

  • ApplicationName1
  • ApplicationName2
  • 下拉-MENU1
  • 下拉-MENU2

我该怎么做?

3 个答案:

答案 0 :(得分:1)

只做

layout.setOrientation(Horizontal)

答案 1 :(得分:0)

我已按照您的要求完成了类似的任务,请查看我的答案: https://stackoverflow.com/a/33339420/5479863

在JSON响应中提供了图像URL,因此在运行时我必须动态地相互添加视图。

看看答案,了解更多...

我希望这会对你有帮助......

快乐编码:)

答案 2 :(得分:0)

尝试以下方法:

1)MActivity.class --------------

public class MActivity extends AppCompatActivity {

private LinearLayout layout_horizontal;
private LinearLayout layout_vertical;
private LayoutInflater layoutInflater;
private FrameLayout fl;

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

    // Frame layout generic container --- root.
    // Frame layout has as a direct child layout_vertical (Linear layout with orientation vertical).
    // for each item inside installedApplicationNames, re-create a linear layout with orientation horizontal (layout_horizontal),
    // with a TextView and Spinner and then add it as a direct child of the linear layout with orientation vertical.

    fl = (FrameLayout) findViewById(R.id.fl);
    layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.layout_vertical , null);
    fl.addView(view);
    layout_vertical = (LinearLayout) view.findViewById(R.id.ll_v);
    populate();
}

private void populate() {

    layout_vertical.removeAllViews();

    List<String> installedApplicationNames = new ArrayList<String>();
    for (int k = 0; k < 30; k++) {
        installedApplicationNames.add("N" + k);
    }

    for (int i = 0; i < installedApplicationNames.size(); i++) {

        View view1 = layoutInflater.inflate(R.layout.layout_horizontal , null);
        layout_horizontal = (LinearLayout) view1.findViewById(R.id.ll_h);

        TextView scrollAppsView = new TextView(this);
        scrollAppsView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT));
        scrollAppsView.setId(i);
        scrollAppsView.setText(installedApplicationNames.get(i) + "\n ");
        layout_horizontal.addView(scrollAppsView);

        // TODO: use a certain method to get vibPatternNames for the current installedApplicationName
        List<String> vibPatternNames = new ArrayList<String>();
        for (int j = 0; j < 4; j++) {
            vibPatternNames.add("S" + j);
        }

        Spinner dropDownMenu = new Spinner(this);
        dropDownMenu.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        dropDownMenu.setId(i);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, vibPatternNames);
        dropDownMenu.setAdapter(dataAdapter);

        layout_horizontal.addView(dropDownMenu);

        layout_vertical.addView(layout_horizontal);
    }

}

}

2)layout.xml -----------

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginStart="5dp"
    android:layout_marginEnd="5dp"
    android:layout_marginTop="5dp"
    android:id="@+id/fl">
</FrameLayout>

</ScrollView>

3)layout_vertical.xml -----------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ll_v"
android:layout_gravity="center"
android:orientation="vertical">


</LinearLayout>

4)layout_horizo​​ntal.xml ---------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/ll_h"
android:layout_gravity="center"
android:layout_height="match_parent"
android:orientation="horizontal">

</LinearLayout>