【问题标题】:How to show both the layout and the message?如何同时显示布局和消息?
【发布时间】:2016-03-10 03:17:06
【问题描述】:

我创建了一个消息活动并且它可以工作,但我的问题是我创建的 xml 布局没有显示,只有消息,反之亦然(布局显示没有消息)。我该如何解决这个问题?

这是类的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String message = intent.getStringExtra (MESSAGE_KEY);
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText (message);
        setContentView(textView);
        setContentView(R.layout.activity_fire_alert);
    }

这是我要显示的xml文件

<RelativeLayout 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: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">

    <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"/>

</RelativeLayout>

【问题讨论】:

    标签: android android-studio


    【解决方案1】:

    您只能拥有 1 个内容视图。您设置的第二个会覆盖另一个。正确的做法是在布局中放置一个 TextView,并将该视图上的文本设置为消息

    【讨论】:

    • 最佳答案。简单而直接。没有代码,只有方向。
    • 我只是编程新手,所以这真的很有用:)
    【解决方案2】:
    <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);
    
    
        }
    

    您只能拥有一组内容视图。尝试用我的更新你的布局和代码并检查它是否会同时显示。

    【讨论】:

    • 有效!所以如果我添加另一个 ImageView 我可以做同样的方法吗?
    • 感谢您接受我的回答,是的,您可以用同样的方式做到这一点,但我希望您阅读有关 android 开发的基础知识并继续前进。
    【解决方案3】:

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-14
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多