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