启动Activity后,布局变得扭曲

时间:2014-09-16 13:58:11

标签: android android-activity

前提是我在android下开发新功能,正如我在主题上写的那样,我在活动之后遇到了问题: http://oi62.tinypic.com/s1rqzl.jpg

这是我用来启动它的代码:

public void connect(View view){
    Intent intent = new Intent(this, MyNewActivity.class);
    startActivity(intent);
}

这是与按钮的onClick事件相关联的方法。 如果有人能解释我的错误,我将不胜感激,谢谢!

这是MainActivity类:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void connect(View view){
        Intent intent = new Intent(this, MyNewActivity.class);
        startActivity(intent);
    }
}

这是MyNewActivity类:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MyNewActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_new);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my_new, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

MainActivity的xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/grey1">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:layout_alignParentTop="true"
    android:background="@drawable/back"
    android:id="@+id/layout_titolo">

    <ImageView
        android:layout_width="230dp"
        android:layout_height="60dp"
        android:src="@drawable/logo"
        android:id="@+id/titolo_imageview"/>
</LinearLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:background="@drawable/grey2">
    <Button
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:text="Connetti"
        android:background="@drawable/back_button"
        android:layout_centerInParent="true"
        android:textColor="#c9ffffff"
        android:textSize="20dp"
        android:onClick="connect"/>
</RelativeLayout>

<EditText
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:id="@+id/nickname"
    android:cursorVisible="true"
    android:textColor="#c9ffffff"
    android:background="@drawable/linea"
    android:hint="Inserisci il nickname"
    android:textSize="22dp"
    android:layout_above="@+id/server"
    android:layout_alignStart="@+id/server"
    android:maxLines="1"/>

<EditText
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:id="@+id/server"
    android:textColor="#c9ffffff"
    android:background="@drawable/linea"
    android:hint="Inserisci il server"
    android:textSize="22dp"
    android:layout_centerVertical="true"
    android:layout_alignStart="@+id/canale"
    android:maxLines="1"/>

<EditText
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:id="@+id/canale"
    android:textColor="#c9ffffff"
    android:background="@drawable/linea"
    android:hint="Inserisci il canale"
    android:textSize="22dp"
    android:layout_below="@+id/server"
    android:layout_centerHorizontal="true"
    android:maxLines="1"/>


</RelativeLayout>

最后是MyNewActivity的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.andrea.myapplication.MyNewActivity">

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

我希望它会有所帮助!

1 个答案:

答案 0 :(得分:0)

非常感谢,我找到了解决方案。不知怎的,编辑器搞砸了,因为只要我删除了xml布局文件并重新制作了一个新文件(与前一个完全相同),它就完美了。我正在使用android studio。

迎接