【问题标题】:Textviews not showing up from array and fragment?Textviews没有从数组和片段中显示?
【发布时间】:2017-03-12 20:53:16
【问题描述】:
 <?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:background="#e3e6e4"
    android:padding="16dp"
    android:paddingTop="30dp"
    android:id="@+id/connections">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:text=" "
        android:background="#007e0e"
        android:id="@+id/textView"/>

</LinearLayout>

我有问题。我写了一个for循环来在数组中添加一个textview,然后将它添加到我的LinearLayout。所有这些都是以编程方式完成的。这些都没有出现。没有语法或运行时错误。但是,在我的 xml 中,我确实包含了一条水平线。这是唯一出现的东西。

package com.example.user.navigationdrawer;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


public class FirstFragment extends Fragment {

    View myView;
    static final String url = "https://www.website.org/";
    ArrayList<String>  outPut = new ArrayList<>();
    LinearLayout diaryLayout;

    final int N = 3; // total number of textviews to add

    TextView[] myTextViews; // create an empty array;
    TextView rowTextView;

    //ArrayList<String> h4 = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.first_layout, container, false);
        diaryLayout = (LinearLayout) myView.findViewById(R.id.connections);
        new Ann().execute();
        return myView;
    }

        private class Ann extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                // Connect to the web site
                Document document = Jsoup.connect(url).get();

                // Get the html document title
                Elements notificationWall = document.select("div[class=flex_column av_one_fifth  flex_column_table_cell av-equal-height-column av-align-top av-zero-column-padding   " +
                    "avia-builder-el-11  el_after_av_one_fifth  el_before_av_one_third]"); //Connect to website.The Notification wall.

                Elements sectionTag = notificationWall.select("section"); //Get section tag.
                Elements contentTitles = sectionTag.select("h4"); // Get H4
                Elements bodyText = sectionTag.select("p"); //Get P
                Elements linkTag = sectionTag.select("a"); //Get links
                Elements images = sectionTag.select("img"); // get image.

                for (int section = 0; section < sectionTag.size(); section++) {
                    outPut.add(sectionTag.get(section).text());
                }
            } catch (IOException e) {
                e.printStackTrace();
            } 

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
        // Set title into TextView
        //here.

            //The problem is here!!!

            myTextViews = new TextView[N];

            for (int i = 0; i < N; i++) {
                // create a new textview
                myTextViews[i] = new TextView(diaryLayout.getContext());

                // set some properties of rowTextView or something
                myTextViews[i].setText("This is row #" + i );


                myTextViews[i].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
                myTextViews[i].setTextSize(14);
                myTextViews[i].setPadding(100,1000,100,100);
                myTextViews[i].setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

                // add the textview to the linearlayout
                diaryLayout.addView(myTextViews[i]);
            }

        }
    }
}

【问题讨论】:

  • 你也有你的 xml 视图的代码吗?您插入 TextView 的视图大小可能存在问题。
  • 为什么不使用LayoutInflater 而不是以编程方式创建文本视图?
  • 我这里加了xml代码

标签: android arrays textview


【解决方案1】:
  1. LinearLayout 方向设置为vertical

    android:orientation = "vertical"
    
  2. 问题可能也与这一行有关:

    myTextViews[i].setPadding(100,1000,100,100);
    

    您的填充太大,这就是文本不可见的原因,请尝试使用较低的值:

    myTextViews[i].setPadding(10, 10, 10, 10);
    

但是

我坚信在这种情况下您应该使用ListViewRecyclerView

【讨论】:

    猜你喜欢
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多