gpt4 book ai didi

java - 以编程方式 android 中 LinearLayout 中微调器和 TextView 的布局对齐

转载 作者:行者123 更新时间:2023-11-29 09:28:28 24 4
gpt4 key购买 nike

我想实现下面的布局,如下图所示...示例 -spinner1 然后是 text1 然后换行 spinner2 然后是 text2 等等....一切都很好我只是想要正确安排布局..我附上了我想要的图像和当前与代码一起显示的图像。

我想要如下图这样的布局

enter image description here

但目前显示如下-

enter image description here

Bookdetails.java

public class Bookdetails extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bookdetails);


final ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("Yes");
spinnerArray.add("No");


final String seat1 = getIntent().getExtras().getString("seat1");
final String seat2 = getIntent().getExtras().getString("seat2");
final String seat3 = getIntent().getExtras().getString("seat3");
final String seat4 = getIntent().getExtras().getString("seat4");
final String seat5 = getIntent().getExtras().getString("seat5");
final String seat6 = getIntent().getExtras().getString("seat6");
Log.i("seats", seat1+seat2+seat3+seat4+seat5+seat6);
Toast toast = Toast.makeText(this, seat1+seat2+seat3+seat4+seat5+seat6, Toast.LENGTH_LONG);
toast.show();

final int N = 6;

final TextView[] myTextViews = new TextView[N];
LinearLayout ll = (LinearLayout) findViewById(R.id.layout);
//setContentView(ll);
if(!(seat1==null)){
final TextView rowTextView = new TextView(this);
rowTextView.setText("Seat selected" + seat1);
ll.addView(rowTextView);

//<------------------------------------------------------------------------------>
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), spinnerArray.get(arg2).toString()+""+seat1,
Toast.LENGTH_SHORT).show();
}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});
ll.addView(spinner);

//<------------------------------------------------------------------------------>
}
if(!(seat2==null)){
final TextView rowTextView = new TextView(this);
rowTextView.setText("Seat selected" + seat2);
ll.addView(rowTextView);
//<------------------------------------------------------------------------------>
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), spinnerArray.get(arg2).toString()+""+seat2,
Toast.LENGTH_SHORT).show();
}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});
ll.addView(spinner);

//<------------------------------------------------------------------------------>

}
if(!(seat3==null)){
final TextView rowTextView = new TextView(this);
rowTextView.setText("Seat selected" + seat3 );
ll.addView(rowTextView);
//<------------------------------------------------------------------------------>
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), spinnerArray.get(arg2).toString()+""+seat3,
Toast.LENGTH_SHORT).show();
}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});
ll.addView(spinner);

//<------------------------------------------------------------------------------>
}
if(!(seat4==null)){
final TextView rowTextView = new TextView(this);
rowTextView.setText("Seat selected"+ seat4 );
ll.addView(rowTextView);
//<------------------------------------------------------------------------------>
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), spinnerArray.get(arg2).toString()+""+seat4,
Toast.LENGTH_SHORT).show();
}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});
ll.addView(spinner);

//<------------------------------------------------------------------------------>
}
if(!(seat5==null)){
final TextView rowTextView = new TextView(this);
rowTextView.setText("Seat selected"+ seat5 );
ll.addView(rowTextView);
//<------------------------------------------------------------------------------>
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), spinnerArray.get(arg2).toString()+""+seat5,
Toast.LENGTH_SHORT).show();
}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});
ll.addView(spinner);

//<------------------------------------------------------------------------------>
}
if(!(seat6==null)){
final TextView rowTextView = new TextView(this);
rowTextView.setText("Seat selected"+ seat6 );
ll.addView(rowTextView);
//<------------------------------------------------------------------------------>
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub

Toast.makeText(getBaseContext(), spinnerArray.get(arg2).toString()+""+seat6,
Toast.LENGTH_SHORT).show();
}

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});
ll.addView(spinner);

//<------------------------------------------------------------------------------>
}

}
}

bookdetails.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:orientation="vertical" >


<LinearLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >


</LinearLayout>

</LinearLayout>

最佳答案

如果您的数据是动态的,则使用 listView 和 spinner、textview 作为 xml 文件中的列表项。

同样从上一个 Activity 到下一个 Activity ,将数据作为可打包数组列表传递。这会有所帮助..

关于java - 以编程方式 android 中 LinearLayout 中微调器和 TextView 的布局对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37274126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com