繁体   English   中英

编程为android,当我构建项目时,我的布局xml文件不会更新

[英]Programming to android, My layout xml files won't update when i build the project

我目前正在测试项目中,以便在开始进行项目之前练习android布局的一些基本功能。 现在,我正在体验一些按钮和活动更改,并且在一个令人沮丧的问题上遇到了麻烦。

一开始我不确定为什么我的新版式不会显示我放置在其上的某些按钮,但是现在问题变得更奇怪了。 正如我将在代码中很快显示的那样,我有两个布局xml文件,一个用于处理主要活动,一个用于处理第二个。 起初一切都很好,但是后来我注意到我看不到第二布局上的唯一按钮,而且我不明白为什么。 我试图将第二个布局更改为主要布局(只是我在main活动中更改setContentView()),但很奇怪的是,程序不断调用原始布局,而不是代码中指定的布局(当然我已经检查了错误,或者如果它实际上已经被构建,确实做到了,我还故意插入了错误并检查程序无法启动)。 看到可能存在更深层次的问题,我尝试将另一个按钮添加到主布局xml文件中,但是对于没有小瓶的情况,该程序始终从旧的开始。

我一直在寻找答案,试图清理我的项目,然后重建它,在字符串xml中寻找丢失的文件,但是没有任何办法可以解决它,对于目前出了什么问题,我目前一无所知。

我确实假设,如果我重新开始该项目,一切都会好起来的,但是,每次出现问题时,我都无法启动一个新项目。 (尤其是如果我做错了)。

这是我的代码:

主要活动:$包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);
}
}

我的第二个活动:包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(用于第二个活动)

<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>

和我最完整的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 ,因此您必须告诉每个视图相对于其他视图或ViewGroup自身的位置。 尝试将android:layout_below="@id/button2"main1.xml的最后一个TextView中。

正如@alextsc指出的那样,在main2.xml您要告诉TextViewfill_parent填充整个屏幕。 将高度更改为wrap_content并在LinearLayout父级中将android:orientation="vertical"设置为默认值horizontal

起初一切都很好,但是后来我注意到我看不到第二布局上的唯一按钮,而且我不明白为什么。

首先,第二个活动的XML似乎不完整。 您至少在这里缺少开头的LinearLayout。 我认为这是存在的,不再有其他丢失(可能只是复制和粘贴错误)

您所做的就是有一个TextView,它的宽度和高度都设置为fill_parent 您看不到按钮的原因是它根本没有空间。 文本视图完全填充了父级LinearLayout (如果您想将其可视化,则将android:background="#ff0000"到文本视图中。背景变为红色并填充屏幕) 对于初学者,您可以将textview的宽度和高度更改为wrap_content ,这应该使按钮可见。 如果您打算为两者分配某种布局,请查看LinearLayouts weight属性。

当谈到不变的布局时:我不确定这里发生了什么。 确保您的XML分别称为main1.xmlmain2.xml 还有一个名为main.xml的默认布局文件。 确保您并非偶然编辑此内容。 (听起来像是让我知道了这一部分,只是为了确保绝对是确定的)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM