以编程方式在相对布局中创建线性布局

Create Linear Layout in Relative Layout Programmatically

我是编程新手,如果这个问题太简单了,我真的很抱歉...

我创建了一个 activity,它使用 putextra 创建了一个意图并打开了第二个 activity。第二个 activity 获取字符串并应在 for 循环中动态创建线性布局。 for 循环的计数器是从 getextra.

解析的 int

问题:循环正常,但是for循环中创建的内容没有显示出来。

XML第二个activity:

<?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>

我把它留得很空,因为我想以编程方式创建线性布局作为相对布局的 child。

这是第二个 activity:

的代码
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中的位置,虽然默认情况下可能会放在左上角

因此,经过大量尝试和失败后,我可以通过在第二个 activity 中创建相对布局来创建设计,向其添加 ListView,定义 ListView XML 并创建自定义 ArrayAdapter.

主要Activity2布局:

<?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);

        }

    }
}

就是这样,现在它可以正常工作了。