[英]Create Linear Layout in Relative Layout Programmatically
我是编程新手,所以我真的很抱歉,如果这个问题太简单了......
我创建了一个活动,它使用 putextra 创建一个意图并打开第二个活动。 第二个活动获取字符串,并应在 for 循环中动态创建线性布局。 for 循环的计数器是从 getextra 解析的 int。
问题:循环正在工作,但在 for 循环中创建的内容没有显示出来。
第二个活动的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity"
android:id="@+id/master">
</RelativeLayout>
我将它保持为空,因为我想以编程方式创建线性布局作为相对布局的子级。
这是第二个活动的代码:
package com.example.stopwatchmulti;
import androidx.annotation.ColorInt;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.zip.Inflater;
public class Main2Activity extends AppCompatActivity {
String ergebnis;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ergebnis = getIntent().getExtras().getString("Ergebnis");
int anzahl = Integer.parseInt(ergebnis);
View master = (View) findViewById(R.id.master);
// Create Dynamic Linear Layout
for (int i = 1; i < anzahl + 1; i++) {
String strI = String.valueOf(i);
Log.i("Loopnumber",strI);
LinearLayout layoutZeile = new LinearLayout(this);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutZeile.setLayoutParams(linearParams);
layoutZeile.setOrientation(LinearLayout.HORIZONTAL);
TextView textViewUserNr = new TextView(this);
textViewUserNr.setTextSize(TypedValue.COMPLEX_UNIT_DIP,20);
textViewUserNr.setTextColor(Color.parseColor("#000000"));
textViewUserNr.setText("User");
layoutZeile.addView(textViewUserNr);
}
}
}
我尝试了几个小时来解决这个问题。 Log.i 说循环本身正在工作。 我还尝试通过使用预定义的线性布局为第二个 xml 充气,但没有任何效果。 我只是得到一个空白屏幕。
你能否尽可能简单地解释你的答案(比如对一个三岁的孩子......)
提前致谢!
看来你忘了放:
master.addView(layoutZeile);
后
layoutZeile.addView(textViewUserNr);
还要换行:
View master = (View) findViewById(R.id.master);
至:
RelativeLayout master = (RelativeLayout) findViewById(R.id.master);
并且还需要定义LinearLayout在RelativeLayout中的放置位置,虽然它可能会默认放置在左上角
因此,经过大量尝试和失败后,我可以通过在第二个活动中创建相对布局来创建设计,向其中添加ListView
,定义ListView
XML 并创建自定义ArrayAdapter
。
主要Activity2
2布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
tools:context=".Main2Activity"
android:id="@+id/master">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:srcCompat="@drawable/carbonbg2"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/customListview"
android:layout_weight="80">
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</RelativeLayout>
接下来我更改了主 class 中的代码:
package com.example.stopwatchmulti;
import androidx.annotation.ColorInt;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.zip.Inflater;
public class Main2Activity extends AppCompatActivity {
//Define Variables and Arrays
String ergebnis;
int anzahl;
ArrayList<String> userNummern = new ArrayList<String>();
ArrayList<String> userNamen = new ArrayList<String>();
ArrayList<String> userZeiten = new ArrayList<String>();
//Create Arrays without giving them a value instantly
String[] userNr;
String[] userName;
String[] userZeit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ergebnis = getIntent().getExtras().getString("Ergebnis");
anzahl = Integer.parseInt(ergebnis);
// Add Lines in favour of UserChoice in First activity
for(int i = 1; i < anzahl + 1; i++) {
userNummern.add(String.valueOf(i));
userNamen.add("User"+String.valueOf(i));
userZeiten.add("00:00:00");
}
// Define Values in the Array by transforming Arrailist elements to the arrays
userNr = userNummern.toArray(new String[0]);
userName = userNamen.toArray(new String[0]);
userZeit = userZeiten.toArray(new String[0]);
// Run Custom ArrayAdapter
ListView customListview = (ListView) findViewById(R.id.customListview);
CustomListview customListview1 = new CustomListview(this,userNr,userName,userZeit);
customListview.setAdapter(customListview1);
}
}
毕竟我用自定义的ArrayAdapter
设置了一个新的 class :
package com.example.stopwatchmulti;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class CustomListview extends ArrayAdapter<String> {
private String[] userNr;
private String[] userName;
private String[] userZeit;
private Activity context;
public CustomListview(Activity context, String[] userNr, String[] userName, String[] userZeit) {
super(context, R.layout.usertableobjects,userNr);
this.context = context;
this.userNr = userNr;
this.userName = userName;
this.userZeit = userZeit;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View r = convertView;
Viewholder viewholder = null;
if (r==null){
LayoutInflater inflater = context.getLayoutInflater();
r=inflater.inflate(R.layout.usertableobjects,null, true);
viewholder=new Viewholder(r);
r.setTag(viewholder);
}
else{
viewholder = (Viewholder) r.getTag();
}
viewholder.tv1.setText(userNr[position]);
viewholder.tv2.setText(userName[position]);
viewholder.tv3.setText(userZeit[position]);
return r;
}
class Viewholder{
TextView tv1;
TextView tv2;
TextView tv3;
Viewholder(View v) {
tv1 = v.findViewById(R.id.textViewNummer);
tv2 = v.findViewById(R.id.textViewName);
tv3 = v.findViewById(R.id.textViewZeit);
}
}
}
就是这样,现在它可以按要求工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.