1. Android中怎麼用純代碼編寫布局
一、用RelativeLayout進行純代碼布局的理論基礎
1、RelativeLayout,顧名思義,就是以「相對」位置/對齊 為基礎的布局方式。
2、android.widget.RelativeLayout 有個繼承自android.view.ViewGroup.LayoutParams 的內嵌類 LayoutParams,使用這個類的實例
調用RelativeLayout.addView 就可以實現「相對布局」。 android.widget.RelativeLayout.LayoutParams 有一個構造函數:
RelativeLayout.LayoutParams(int w, int h),參數指定了子 View 的寬度和高度,這一點和其父類是一樣的。而實現相對布局的關
鍵在它的 兩個 addRule 方法上。anchor 參數指定可以是View 的 id(「相對於誰」)、RelativeLayout.TRUE(啟用某種對齊方式) 或者
是-1(應用於某些不需要 anchor 的 verb);AddRule 方法的 verb 參數指定相對的「動作」(以下常量均定義於
android.widget.RelativeLayout中,為了簡便不給出其全名):
3、ALIGN_BOTTOM、ALIGN_LEFT、 ALIGN_RIGHT、 ALIGN_TOP: 本 View 的 底邊/左邊/右邊/頂邊 和 anchor 指定的 View 的
底邊/左邊/右邊/頂邊 對齊。
ALIGN_WITH_PARENT_BOTTOM 、ALIGN_WITH_PARENT_LEFT 、 ALIGN_WITH_PARENT_RIGHT 、
ALIGN_WITH_PARENT_TOP : 和上面一組常量類似,只不過不需要再指定 anchor, 其 anchor 自動為 Parent View。
CENTER_HORIZONTAL、CENTER_IN_PARENT 、CENTER_VERTICAL : 如果 anchor 為 TRUE,在 Parent 中 水平居中/水平
和垂直均居中/垂直居中。
POSITION_ABOVE 、POSITION_BELOW 、 POSITION_TO_LEFT 、POSITION_TO_RIGHT : 本 View 位於 anchor 指定的 View
的上邊/下邊/左邊/右邊。
2. android如何在代碼里設置布局
java">//定義一個線性布局
LindearLayouttp=newLindearLayout(this);
//定義一個布局參數類(用於定義Button在線性布局中的參數)
LayoutParamsltp=newLayoutParams(LayoutParams.WARP_CONTENT,LayoutParams.WARP_CONTENT);
Buttonlbt=newButton(this);
tp.addView(lbt,ltp);//將內Button加入到線性布局中。
//但是容不推薦代碼里實現,因為android是MVC開發模式,數據操作,UI,代碼等實現都是分開寫的,這樣寫的好處你多編點代碼就會體會到了。雖然靠純代碼甚至是在一個activity中都可能完成一個app的編寫,但是這樣就完全沒了可維護性了
3. 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處的效果是一樣的。
4. android tabbar 怎麼添加布局進去
如下代碼:
LinearLayout layout = new LinearLayout(this);
TextView tx = new TextView(this);
tx.setText('我是動態添加的');
layout.addView(tx);
setContentView(layout);
這就動態添加了一個線性布局,並且在布局裡面加了一個textview