我目前正在开发一个测试项目,以便在开始我的项目之前练习 Android 布局的一些基本功能。 目前,我遇到了一些按钮和 Activity 更改,并遇到了一个令人沮丧的问题。
起初我不确定为什么我的新布局不会显示我放置在其上的一些按钮,但现在问题变得更奇怪了。 正如我很快将在代码中展示的那样,我有两个布局 xml 文件,一个用于处理主要 Activity ,另一个用于处理第二个 Activity 。起初一切都很好,但后来我注意到我看不到第二个布局上的单独按钮,我不明白为什么。我尝试将第二个布局更改为主布局(只是我更改了主 Activity 中的 setContentView() ),但奇怪的是,程序不断调用原始布局而不是代码中指定的布局(当然我已经检查了错误,或者如果它实际上已经构建了,它确实如此,我还故意插入了错误并检查程序是否无法启动)。 看到可能存在一些更深层次的问题,我尝试在主布局 xml 文件中添加另一个按钮,但由于没有小瓶,程序一直从旧按钮开始。
我已经寻找答案,尝试清理我的项目并重建它,在我的字符串 xml 中查找丢失的文件,但没有任何方法可以修复它,我目前对出了什么问题一无所知。
我确实认为,如果我重新开始该项目,一切都会好起来,但我不能每次出现问题时都开始一个新项目。 (特别是如果我做错了什么)。
这是我的代码:
主要 Activity : $ 包test.android.mark.III;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
public class AndroidMarkIII extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
final Button button1 = (Button) findViewById(R.id.button1); // bind button to the view from the xml
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openNewWindow(v);
}
});
final Button button2 = (Button) findViewById(R.id.button2);// bind button to the view from the xml
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openPopupWindow(button2);
}
});
}
protected void openNewWindow(View v) {
Intent listWindowIntent = new Intent(v.getContext(), ListWindowActivity.class);
startActivityForResult(listWindowIntent, 0);
}
}
我的第二项 Activity : 包test.android.mark.III;
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;
public class ListWindowActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button returnButton = (Button) findViewById(R.id.return_button);
returnButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
我的主要 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/title1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title1"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title1"
android:layout_centerHorizontal="true"
android:text="@string/push_button"
android:padding="50dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:text="@string/popup_button"
android:layout_centerHorizontal="true"
android:padding="75dp" />
<TextView //Was added later for check reason (wasn't seen on any run)
android:id="@+id/end"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title1"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
我的第二个 XML(用于第二个 Activity )
<TextView
android:id="@+id/list_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello World" />
<Button
android:id="@+id/return_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/return_button"
android:visibility="visible">
</Button>
</LinearLayout>
无论如何,我的字符串 xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AndroidMarkIIIActivity!</string>
<string name="app_name">AndroidMarkIII</string>
<string name="title1">This is a test program</string>
<string name="push_button">Push Button - 50dp</string>
<string name="popup_button">Popup Button - 75dp</string>
<string name="return_button">return Button - 10dp</string>
<string name="close_popup_button">X</string>
<string name="popup_text">this is popup - 25dp</string>
<string name="list_window_app_name">AndroidMarkIII2ndWindow</string>
</resources>
和我的 manfiest xml
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".AndroidMarkIII" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ListWindowActivity">
</activity>
</application>
</manifest>
我真的希望有人能够解决这个问题,因为它看起来实际上是一个非常愚蠢的问题,但我仍然无法在遇到它时继续工作。
提前致谢 奥伦
最佳答案
出现此问题的原因是您的 XML 布局不正确。
在 main1.xml
中,您使用的是 RelativeLayout
,因此您必须告诉每个 View 相对于其他 View 或 ViewGroup 的放置位置本身。尝试将 android:layout_below="@id/button2"
添加到 main1.xml
中的最后一个 TextView
。
正如 @alextsc 指出的,在 main2.xml
中,您告诉 TextView
用 fill_parent
填充整个屏幕。将高度更改为 wrap_content
并将 LinearLayout
父级中的 android:orientation="vertical"
设置为 the default is horizontal
.
关于java - 对 android 编程,当我构建项目时,我的布局 xml 文件不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8368979/