Java 通过android编程,我的布局xml文件赢得了';我构建项目时不会更新
我目前正在做一个测试项目,以便在开始我的项目之前练习android布局的一些基本功能。 目前,我正在经历一些按钮和活动的变化,并在一个令人沮丧的问题上取得了进展Java 通过android编程,我的布局xml文件赢得了';我构建项目时不会更新,java,android,xml,eclipse,layout,Java,Android,Xml,Eclipse,Layout,我目前正在做一个测试项目,以便在开始我的项目之前练习android布局的一些基本功能。 目前,我正在经历一些按钮和活动的变化,并在一个令人沮丧的问题上取得了进展 <TextView android:id="@+id/list_text" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="Hello World" /> <But
<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>
<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文件,一个用于处理主要活动,另一个用于处理第二个活动。起初一切都很好,但后来我注意到我看不到第二个布局上的单独按钮,我不明白为什么。我试图将第二个布局更改为主布局(只是在主活动中更改setContentView()),但奇怪的是,程序一直调用原始布局,而不是代码中指定的布局(当然,我已经检查了错误,或者如果它实际上是构建的,它确实检查了,我还特意插入了错误,并检查程序是否无法启动)。
考虑到可能存在一些更深层次的问题,我尝试在主布局xml文件中添加另一个按钮,但由于没有小瓶,程序一直从旧按钮开始
<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>
<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中寻找丢失的文件,但没有任何东西可以修复它,目前我对出错的地方一无所知
<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>
<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>
我确实认为,如果我重新开始这个项目,一切都会好起来,但我不能每次问题出现时都开始一个新项目(特别是如果我做错了什么)
<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>
<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>
这是我的密码:
<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>
<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>
主要活动:
$
软件包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);
}
}
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();
}
});
}
}
<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>
<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>
我的第二项活动:
软件包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);
}
}
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();
}
});
}
}
<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>
<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文件:
<?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>
<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>
<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>
起初一切都很好,但后来我注意到我看不到第二个布局上的单独按钮,我不明白为什么
<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>
<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似乎不完整。您至少缺少了此处的开头线性布局。我假设这一点存在,并且没有其他遗漏(可能只是复制粘贴错误)
<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>
<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>
你所做的是在宽度和高度上都设置为fill_parent
的文本视图。你看不到按钮的原因是没有足够的空间容纳它。文本视图完全填充了父线性布局(如果你想将其可视化,请添加android:background=“#ff0000”
到文本视图。它的背景变为红色并填满屏幕)。对于初学者,您可以将文本视图的宽度和高度更改为包裹内容
,这将使按钮可见。如果您打算为这两个视图指定某个布局分布,请查看LinearLayouts权重属性
<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>
<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
和main2.xml
。还有一个默认布局文件名为main.xml
。请确保您没有意外编辑此文件。(听上去,为了确保绝对可靠,我已经涵盖了这一部分)问题的出现是因为您的XML布局不正确
<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>
<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>
在main1.xml
中,您使用的是RelativeLayout
,因此您必须告诉每个视图相对于其他视图或ViewGroup
自身的位置。尝试将android:layout_lower=“@id/button2”
添加到main1.xml
中的最后一个TextView
<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>
<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>
正如@alextsc指出的,在main2.xml
中,您告诉TextView
用fill\u parent
填充整个屏幕。将高度更改为wrap\u content
并设置android:orientation=“vertical”
在线性布局中
父组件。我有时会遇到类似这样的奇怪问题,后来发现这完全是eclipse的错(有时)。如果清理
或重新启动没有帮助,我有时会创建一个新项目并从原始项目复制所有内容。GLAt乍一看,代码似乎很好。除了您的第二个xml缺少开头,非常感谢,这解决了所有问题,让我觉得有点愚蠢。
<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>
<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>