androidjavaxml
『壹』 android開發中,xml和java代碼的各自負責什麼功能
xml負責界面,視圖,java代碼負責邏輯處理。其實xml只是承載一些視圖信息,運行程序時,java代碼會解析xml文件,繪制出界面。就算沒有xml,在代碼中也可以動態添加視圖,在xml寫視圖,更加方便開發
『貳』 如何在android的XML和java代碼中引用字元串常量
xml文件格式:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="rfid_mgs_error_config">Device configuration error</string>
</resources>
調用:
getString(R.string.rfid_msg_type)
『叄』 android開發中,xml如何與.java文件關聯起來
比如,你寫了一個名為main.xml的文佳(是用於界面形式的顯示),然後想在Activity01裡面去調用,那麼你需要在Activity01的onCreate主方法中用: setContentView(R.layout.main);這句話就是用main.xml作為Activity01的界面效果。
如果,你需要對main裡面的那個按鈕進行監聽,需要通過按鈕id找到按鈕。使用語句:Button button01 = (Button) findViewById(R.id.xxx) ,這里的xxx是值main中你需要監聽的按鈕的id。
『肆』 新手,android下怎麼全是xml文件,java代碼應該寫在什麼地方
對於Android來說xml一般是布局文件,或者是配置文件,java代碼一般是src文件夾下的。
如圖
src就是你編寫java代碼的地方
layout就是xml布局文件
values也是xml文件,但是他是string等變數文件
『伍』 android 如何利用java代碼 在一個xml布局中插入另一個xml布局
Android在xml文件中可使用include包含其他定義好的布局, 可以將多處用到的布局單獨出來,然後用include包含進來,這種包含方法相當於把原來布局的一部分代碼獨立出來,供大家共同使用,也就相當於面向對向中的類的概念差不多。下面我們逐步講解include的作用。
先看下我們要實現的整體界面:
一、未使用Include時
通常情況下,我們直接就能寫出布局代碼,下面是所使用的XML代碼:
[html]view plain
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!--第一部分-->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="第一個BTN"/>
<Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OneButton"/>
<!--第二部分-->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:text="第二個BTN"/>
<Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SecondButton"/>
<!--最後的按鈕-->
<Button
android:id="@+id/another"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AnotherButton"/>
</LinearLayout>
這段代碼理解起來一點難度沒有,就是幾個TextView和幾個Button,下面我們用include把這段代碼給分割成幾個文件,並完成相同的效果;
二、使用Include時
1、先將上面代碼標記有「第一部分」的,代碼段分離成一個文件(sublayout1.xml);
[html]view plain
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#505050"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:text="第一個BTN"/>
<Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OneButton"/>
</LinearLayout>
2、再將標記有「第二部分」的代碼段,分離成第二個文件(sublayout2.xml):
[html]view plain
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:text="第二個BTN"/>
<Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SecondButton"/>
</LinearLayout>
3、主文件中使用include,將上面兩個文件包含進去(activity_main.xml);
[html]view plain
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<include
android:id="@+id/main1"
layout="@layout/sublayout1"/>
<include
android:id="@+id/main2"
layout="@layout/sublayout2"/>
<Button
android:id="@+id/another"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AnotherButton"/>
</LinearLayout>
這樣就實現了相同的效果,這里可以看到,include並沒有其它的功能,只是把一個XML布局引入進來當做自己的布局,跟直接把引用的這段代碼寫在include處的效果是一樣的。
『陸』 在android中怎樣在java類里修改xml文件中的內容
你可以是用java自己的sax,dom進行xml文件解析,在去修改文件內容,不過推薦你使用Android自帶的pull解析xml文件,很簡單就解決了。