<?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]);
}
}
}
}
将LinearLayout
方向设置为vertical
:
android:orientation = "vertical"
问题可能也与这条线有关:
myTextViews[i].setPadding(100,1000,100,100);
您的填充太大,这就是文本不可见的原因,请尝试使用较低的值:
myTextViews[i].setPadding(10, 10, 10, 10);
但
我坚信在这种情况下您应该使用ListView或RecyclerView。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句