我有 2 个 TextView
,但其中一个不显示任何文本。我在 LogCat
中看到它在那里,但在应用程序上它没有显示。我确定问题出在我的 xml
文件中,但我不知 Prop 体是什么。我在这里也看到了很多这样的帖子,我尝试了与他们不同的东西,但没有帮助我。这是布局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="320dp"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_weight="10.18"
android:background="@drawable/buttons"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="" />
<TextView
android:id="@+id/text"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:layout_marginTop="10dp"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="300dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<Button
android:id="@+id/pressMe"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:background="@drawable/bg_button"
android:gravity="center"
android:padding="4dp"
android:text="@string/PressMe" />
<Button
android:id="@+id/dontPress"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/bg_button"
android:gravity="center"
android:padding="4dp"
android:text="@string/DontPressMe" />
</LinearLayout>
</LinearLayout>
</ScrollView>
有问题的 TextView
是 "@+id/name"
我还通过 putExtra
和 getExtra
从另一个 Activity 中获取它们
intent.putExtra("text", stocks[position].text);
intent.putExtra("name", stocks[position].name);
和
Bundle b = getIntent().getExtras();
if (b != null) {
name = b.getString("name");
textView.setText(name+"");
text = b.getString("text");
textView.setText(text+"");
所以 text
在那里,但是 name
不见了。在第一个 Activity 中,两者都在那里。
textView = (TextView) findViewById(R.id.name);
textView = (TextView) findViewById(R.id.text);
最佳答案
你的Layout没有问题
但问题出在您的代码中,您必须在 oncreate 方法中找到这两个 textView。 你确实找到了两者,但问题是当你设置文本时,使用相同的文本引用
在这里你对两个 TextView 采用相同的引用。像这样改变它。
TextView txtName = (TextView)findviewbyid(R.id.name);
TextView txtText = (TextView)findviewbyid(R.id.text);
Bundle b = getIntent().getExtras();
if (b != null) {
name = b.getString("name");
//change here txtName.setText(name+"");
text = b.getString("text");
//change here txtText.setText(text+"");
}
关于android - TextView 不显示任何文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26927273/