我正在编写我的第一个Android应用程序,并最终设法让我的程序显示在AVD上。但是,GUI似乎在同一窗口中出现两次。第一个在另一个之上。
它看起来像这样: http://i.stack.imgur.com/0xK1S.png
我的主要活动如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark_gray"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.xsiand.something.StartingPoint$PlaceholderFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/red"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:paddingRight="2dp"
android:text="Your name:"
android:textColor="@color/white"
android:textSize="30dp" />
<EditText
android:id="@+id/nameField"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:ems="10"
android:inputType="textPersonName"
android:textColor="@color/white" />
</LinearLayout>
<Button
android:id="@+id/saveButton"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:text="@string/save" />
<TextView
android:id="@+id/textResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your name is: "
android:textColor="@color/white"
android:textSize="30dp" />
</LinearLayout>
我唯一的课程是这样的:
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class StartingPoint extends Activity {
private String name;
private Button saveButton;
private TextView display;
private EditText nameInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
name = null;
nameInput = (EditText)findViewById(R.id.nameField);
saveButton = (Button)findViewById(R.id.saveButton);
display = (TextView)findViewById(R.id.textResult);
nameInput.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//also what to put here in order to get the keyboard to appear?
}
});
saveButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(nameInput.getText().toString() != null){
name = nameInput.getText().toString();
display.setText("Your name is: " + name);
}
else
display.setText("noname");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starting_point, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
return rootView;
}
}
}
最后我的清单看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xsiand.something"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".StartingPoint"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
为什么gui出现两次?
答案 0 :(得分:0)
您正在活动中添加PlaceholderFragment。
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
并且PlaceholderFragment正在为您的活动提供相同的布局
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
return rootView;
}