非技術問題
- 為什么看好Android?
- 以前是否從事過Android的工作,做過哪些工作?
- 你做過的最復雜的界面是什么?
- 如何解決Android學習中遇到的難題?
- Android只能在手機或其他移動設備上使用嗎?
Android的基本概念
- Android的特點有哪些?
- MVC格式
- Android的系統構架
開發Android程序
- 搭建Android環境
- Android開發環境的使用
布局
Android中的布局
- FrameLayout(堆棧布局) *
類似于Photoshop的圖層布局 - LinearLayout(線性布局) *
將多個View水平或垂直排列 - RelativeLayout(相對布局) *
通過確定兩個或多個組件的相對位置來拜訪組件 - TableLayout(表格布局)
將View按表格形式排列 - AbsoluteLayout(絕對布局)
設置View的絕對坐標,不建議使用。
布局使用技巧
- FrameLayout布局
- FrameLayout的主要用途
FrameLayout主要用于進行層次結構的布局。 - 如何實現上、下、左、右、居中對齊?
android:layout_gravity - 在FrameLayout中后一個View會覆蓋前一個View?
不正確。在FrameLayout中,只有所有的View同樣大小,并且在同一個位置上才會被覆蓋。類似于PhotoShop,未被覆蓋的地方會顯示出來。
- LinearLayout布局
- 如何獲得LinearLayout的寬度和高度?
由于Android的運行機制決定了無法在組件類外部使用getWidth和getHeight。如果想直接獲取在布局文件中定義的組件的寬度和高度,可以直接使用View.getLayoutParams().width()和View.getLayoutParams().height。 - 如何在多個LinearLayout中添加分隔線?
適合所有Android版本:在多個LinearLayout放置用于顯示分隔線的View。
Android 3.0 及以上版本:android:showDividers=none/beginning/end/middle.+android:divider(設置一個Drawable ID)
在Java代碼中:linearLayout.setShowDividers|linearLayout.setDividerDrawable. - 如何實現左、右、居中對齊?
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="fill_parent"
- RelativeLayout布局
如何確定Button相對于手機屏幕的位置坐標?
View view = findViewById(R.id.button1);
int[] locations = new int[2];
view.getLocationOnScreen(locations);
int x = location[0];
int y = location[1];-
如何在Java代碼中完成RelativeLayout中設置標簽的android:layout_toLeftOf,android:layout_toRightOf功能?
RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.main, null);
Button button = (Button) getLayoutInflater.inflate(R.layout.button, null);RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.button1); layoutParams.addRule(RelativeLayout.BELOW, R.id.button1); button.setParams(layoutParams); relativeLayout.addView(button);
如何動態改變RelativeLayout中按鈕的布局?
首先獲得按鈕對象,然后使用LayoutParams.addRule設置相應的屬性。
- TableLayout布局
- 請描述一下TableLayout布局的用法。
TableLayout中嵌套TableRow,TableRow中有不定數目的View,每個View代表一列,每個TableRow代表一行。 - <TableLayout>標簽中的strechColumns屬性的作用是什么?如何使用strechColumns屬性?
用于要拉伸的列的索引。如果指定多個索引列,中間用逗號分隔。
- AbsoluteLayout布局
- 介紹一下AbsoluteLayout布局的用法。
絕對布局(坐標布局),View在布局中通過坐標定位。layout_x,layout_y. - 如何動態改變AbsoluteLayout布局中View的位置?
View view = findViewById(R.id.imageView);
Layout.LayoutParams layout = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 300, 300);
view.tParams(LayoutParams);
- 將布局存成圖像
Android SDK提供了API允許直接將可視組件繪制在Bitmap對象上。
繪制可視組件主要涉及到View.setDrawingCacheEnabled
,View.getDrawableCache
方法。
- 如何將當前界面的可視組件以同樣的相對位置和大小保存在png圖像中?
View view = getLayoutInflater().inflate(R.layout.main, null);
View.setDrawingCacheEnabled(true);
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasureWidth(), view.getMeasureHeight());
try {
Bitmap bitmap = view.getDrawingCache();
FileOutoutStream fos = new FileOutoutStream("/sdcard/test.png");
bitmap.compress(CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) { } - 如何將Android應用程序窗口的背景色設成漸變色?
GradientDrawable
類
GradientDrawable gradientDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] {Color.RED, Color.YELLOW});
getWindow().setBackgroundDrawable(gradientDrawable);
布局屬性
- android:layout_weight屬性
用于設置組件的重要程度。重要程度越高,值越低。 - android:padding和android:layout_margin屬性
android:padding用于設置View中的內容在上、下、左、右四個方向距離邊緣的距離。
android:layout_margin用于設置View距離其他View或父容器邊緣的距離。
高級布局技術
- 如何重用布局文件?
<include>標簽引用其他的布局文件,并用android:id屬性覆蓋被引用布局文件中頂層節點的android:id屬性值。 - 如何查看apk文件中的布局文件源碼呢?
先將apk文件解壓,再使用AXMLPrinter2工具對XML布局文件反編譯。