简体   繁体   English

使用现有textview设计以编程方式在另一个textview下面显示textview

[英]Displaying textview below another textview programically using existing textview design

I have a Textview design in activity_main.xml which I want to use for displaying one textview below another. 我在activity_main.xml中有一个Textview设计,我想将其用于在另一个下面显示一个textview。 I got this code from stackoverlow itself and made a little change to use the existing Textview design. 我从stackoverlow本身获得了这段代码,并做了一些改动以使用现有的Textview设计。 But the app closes when opened displaying nothing. 但是,该应用程序在打开时关闭,什么也不显示。 I'm very new to Android Development. 我是Android开发的新手。

XML code for TextView: TextView的XML代码:

<TextView
    android:id="@+id/textviews"
    android:layout_width="match_parent"
    android:layout_height="104dp"
    android:textSize="30sp" />

Here is the code I got from Stackoverflow: 这是我从Stackoverflow获得的代码:

String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);        
for( int i = 0; i < textArray.length; i++ )
{
TextView textView = new TextView(this);
textView.setText(textArray[i]);
linearLayout.addView(textView);
}

This code runs perfectly with no issues. 这段代码可以完美运行,没有任何问题。 But when I tried calling the existing textview design by using findViewById, the code builds but the app never opens. 但是,当我尝试使用findViewById调用现有的textview设计时,代码会生成,但该应用程序永远不会打开。 Here it is: 这里是:

String[] textArray = {"One", "Two", "Three", "Four"};
    LinearLayout linearLayout = new LinearLayout(this);
    setContentView(linearLayout);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    for( int i = 0; i < textArray.length; i++ )
    {
        TextView textView =(TextView) findViewById(R.id.textviews);
        textView.setText(textArray[i]);
        linearLayout.addView(textView);
    }

How do I use the existing textview design in this code? 如何在此代码中使用现有的textview设计?

Solution: 解:

You just have a slight change in your code, Compare it from below code and try: 您只对代码做了些微更改,请从下面的代码中进行比较,然后尝试:

String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);

for( int i = 0; i < textArray.length; i++ )
{
    View v = (View) getLayoutInflater().inflate(R.layout.your_text_view_layout, null);
    TextView textView = v.findViewById(R.id.textviews);
    textView.setText(textArray[i]);
    linearLayout.addView(textView);
}

Hope it works. 希望它能工作。

You have to inflate TextView Layout, please try below code, 您必须为TextView Layout充气,请尝试以下代码,

textviews.xml textviews.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textviews"
    android:layout_width="match_parent"
    android:layout_height="104dp"
    android:textSize="30sp" />

Java Code, Java代码

String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for( int i = 0; i < textArray.length; i++)
{
    TextView textView = (TextView) getLayoutInflater().inflate(R.layout.textviews, null);
    textView.setText(textArray[i]);
    linearLayout.addView(textView);
}

Take Linear Layout in xml, 以xml中的线性布局,

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

and add textview in it as 并在其中添加textview为

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linear);

        String[] textArray = {"One", "Two", "Three", "Four"};

        for( int i = 0; i < textArray.length; i++ )
        {
            TextView textView = new TextView(this);
            textView.setText(textArray[i]);
            linearLayout.addView(textView);
        }

As you want TextView below the TextView it has to be done by using ListView. 如要在TextView下方找到TextView,必须使用ListView完成。 First, you have to make a custom adapter layout. 首先,您必须进行自定义适配器布局。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#cede8a">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:padding="10dp"
    android:id="@+id/text"
    android:textColor="#000000"
    />
</LinearLayout>

And then make a list view in your layout where you want to show the TextView. 然后在要显示TextView的布局中创建一个列表视图。

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

In main class you initialise the ListView and store the data in array or other data structure. 在主类中,初始化ListView并将数据存储在数组或其他数据结构中。

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private ArrayList<String> al_data = new ArrayList<>();
    private Adapter obj_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.listview);
        al_data.add("1234");
        al_data.add("1257");
        al_data.add("100");
        al_data.add("1547");

        obj_adapter = new Adapter(al_data);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false);
        listView.setLayoutManager(layoutManager);
        listView.setAdapter(obj_adapter);
    }
}

And now in the class of the custom adapter write this code. 现在,在自定义适配器的类中编写此代码。

public class Adapter extends ListView.Adapter<Adapter.MyViewHolder>{

    private ArrayList<String> al_data;

    public class MyViewHolder extends ListView.ViewHolder{
        public TextView textview;

        public MyViewHolder (View view){
            super(view);
            textview = view.findViewById(R.id.text);
        }
    }

    public Adapter(ArrayList<String> al_data) {
        this.al_data = al_data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_layout,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        holder.textview.setText(al_data.get(position));
    }

    @Override
    public int getItemCount() {
        return al_data.size();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM