Android 如何以编程方式在LinearLayout中相邻添加两个视图?
我已经看到了一些关于如何使用XML在Android 如何以编程方式在LinearLayout中相邻添加两个视图?,android,android-linearlayout,Android,Android Linearlayout,我已经看到了一些关于如何使用XML在LinearLayout中添加两个相邻视图的说明。如何通过编程实现这一点 我有以下两种方法: private void createListOfInstalledApplications() { installedApplicationNames = this.getApplicationNames(); installedPackages = this.getPackageNames(); for(int i = 0; i <
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);
}
}
下拉菜单现在显示在应用程序名称下面。但是,我希望它们出现在右边。所以
“应用程序名称1下拉菜单1”
而不是
- 应用程序名称1
- 应用程序名称2
- 下拉菜单1
- 下拉菜单2
我该怎么做?我已经按照您的要求完成了类似的任务,请看我的答案: 在JSON响应中,提供了图像URL,因此在运行时,我必须动态地添加彼此相邻的视图 看答案了解更多 我希望这能帮助你 快乐编码:)就这么做吧
layout.setOrientation(Horizontal)
请尝试以下操作:
1) MActivity.class--------------
公共类MActivity扩展了appcompativity{
私人线路平面布置图;
私人线路布局图(垂直);
私人停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场停车场;
私有框架布局fl;
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
//框架布局通用容器---根。
//框架布局作为直接子布局_垂直(方向垂直的线性布局)。
//对于installedApplicationNames中的每个项目,重新创建方向为水平的线性布局(布局为水平),
//使用TextView和微调器,然后将其添加为具有垂直方向的线性布局的直接子级。
fl=(框架布局)findViewById(R.id.fl);
layoutInflater=(layoutInflater)getApplicationContext().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
视图=布局平坦。充气(R.layout.LAYOU垂直,空);
fl.addView(视图);
布局垂直=(线性布局)视图。findViewById(R.id.ll\u v);
填充();
}
私有void填充(){
布局_垂直。移除所有视图();
List installedApplicationNames=new ArrayList();
对于(int k=0;k<30;k++){
已安装的应用程序名称。添加(“N”+k);
}
对于(int i=0;i
2) layout.xml-----------
3) layout_vertical.xml-----------
4) layout_horizontal.xml---------
为什么不使用水平*布局管理器?这样,您只需要设计1个ImageView,布局管理器就会将它们放在下一个旁边
layout.setOrientation(Horizontal)
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);
}
}
}
<?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>
<?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>
<?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>