我创建了一个消息活动并且它可以工作,但我的问题是我创建的xml布局没有显示,只有消息,反之亦然(布局显示没有消息)。我如何解决这个问题?
这是该类的代码:
Add-Type -assembly "System.IO.Compression"
Add-Type -assembly "System.IO.Compression.Filesystem"
这是我要显示的xml文件
{{1}}
答案 0 :(得分:1)
您只能拥有1个内容视图。你设置的第二个覆盖另一个。正确的方法是在布局中使用TextView,并将该视图上的文本设置为消息
答案 1 :(得分:0)
setContentView()
方法,顾名思义,设置您的活动内容。要解决此问题,请将TextView
直接放在布局文件中,例如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
...>
<Button
android:layout_width="wrap_content"
android:layout_height="
...
.../>
<TextView
android:id="@+id/text_view_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
在Java代码上,使用刚刚在布局上创建的ID创建TextView
,然后在其上设置消息文本。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fire_alert);
TextView textView = (TextView) findViewById(R.text_view_message);
Intent intent = getIntent();
String message = intent.getStringExtra (MESSAGE_KEY);
textView.setText (message);
}
答案 2 :(得分:0)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation:"vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.user.tictactoeniandy.FireAlertActivity">
<TextView
android:id="@+id/loc_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="View Sender Location"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="View Sender Location"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp"
android:id="@+id/loc_button1"/>
</LinearLayout>
并在您的活动代码中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fire_alert);
Intent intent = getIntent();
String message = intent.getStringExtra (MESSAGE_KEY);
TextView textView = (TextView)findviewbyid(R.id.loc_textview);
textView.setTextSize(40);
textView.setText (message);
}
您只能拥有一个设置内容视图。尝试使用我的更新您的布局和代码,并检查它是否会显示。