Android 文本视图没有从数组和片段中显示?
将Android 文本视图没有从数组和片段中显示?,android,arrays,textview,Android,Arrays,Textview,将线性布局方向设置为垂直: 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;
线性布局
方向设置为垂直
:
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]);
}
}
}
}
android:orientation = "vertical"
填充太大,因此文本不可见,请尝试使用较小的值:
myTextViews[i].setPadding(100,1000,100,100);
我坚信在这种情况下应该使用or。您是否也有xml视图的代码?您插入文本视图的视图的大小可能有问题。为什么不使用
LayoutInflater
而不是通过编程创建文本视图呢?我在这里添加了xml代码。
myTextViews[i].setPadding(100,1000,100,100);
myTextViews[i].setPadding(10, 10, 10, 10);