布局元素没有显示出来

时间:2011-05-27 14:28:34

标签: android xml layout

我有两个活动,第一个是正确生成,但第二个不是。只显示xml文件中的第一个元素。这是我的xml文件:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/alternateActivity"
    android:shadowColor="@color/shadowColor"></TextView>
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/oldActivityButton"
    android:text="@string/oldActivityButton"></Button>
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/changeText"
    android:text="@string/changeTextButton"></Button>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/originalText" 
    android:id="@+id/textToChange"></TextView>
</LinearLayout>

这是我的活动对应:

package fsg.dev.activitytest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class newActivity extends Activity {
private Button oldActivityButton;
private Button changeText;
private TextView textToChange;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alternate);

        oldActivityButton = (Button) findViewById(R.id.oldActivityButton);
        changeText = (Button) findViewById(R.id.changeText);
        textToChange = (TextView) findViewById(R.id.textToChange);

        oldActivityButton.setOnClickListener(new OnClickListener(){
        public void onClick(View view){
            changeActivity();
        }
        });
        changeText.setOnClickListener(new OnClickListener(){
        public void onClick(View view){
            changeText();
        }
        });
    }

    private void changeActivity(){
        Intent i = new Intent(this, activityTest.class);
        startActivity(i);
    }

    private void changeText(){
        if(textToChange.equals(R.string.originalText)){
        textToChange.setText(R.string.newText);
        } else {
        textToChange.setText(R.string.originalText);
        }
    }
}

还有其他人看过这个问题吗?或知道解决问题的方法?

2 个答案:

答案 0 :(得分:5)

尝试将android:orientation =“vertical”添加到LinearLayout

答案 1 :(得分:-1)

替换

Intent i = new Intent(this, activityTest.class);

通过

Intent i = new Intent().setClass(this, activityTest.class);