界面开发基础
Introduction
Material Design
- com.android.support:appcompat-v7:23.1.0
- com.android.support:design :23.1.0
总而言之,如果使用
ShowCase
plaid
Screen & Size
参考资料
DIP(Device Independent Pixels,dp)
概念 | 解释 |
---|---|
屏幕尺寸 |
即显示屏幕的实际大小,按照屏幕的对角线进行测量。为简单起见, |
屏幕长宽比 |
长宽比是屏幕的物理宽度与物理高度的比例关系。应用程序可以通过使用限定的资源来为指定的长宽比提供屏幕布局资源, |
屏幕分辨率 |
在屏幕上显示的物理像素总和。需要注意的是:尽管分辨率通常用宽 |
密度 |
根据像素分辨率,在屏幕指定物理宽高范围内能显示的像素数量。在同样的宽高区域,低密度的显示屏能显示的像素较少,而高密度的显示屏则能显示更多的像素。屏幕密度非常重要,因为其它条件不变的情况下,一共宽高固定的 |
密度无关的像素 |
指一个抽象意义上的像素,程序用它来定义界面元素。它作为一个与实际密度无关的单位,帮助程序员构建一个布局方案 |
尺寸获取
#获取基本属性信息
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels; // 屏幕宽度(像素)
int height = metric.heightPixels; // 屏幕高度(像素)
float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5)
int densityDpi = metric.densityDpi; // 屏幕密度DPI(120 / 160 / 240)
// 获取屏幕密度(方法1)
int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px)
int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800p)
Log.e(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// 获取屏幕密度(方法2)
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
float xdpi = dm.xdpi;
float ydpi = dm.ydpi;
Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
screenWidth = dm.widthPixels; // 屏幕宽(像素,如:480px)
screenHeight = dm.heightPixels; // 屏幕高(像素,如:800px)
Log.e(TAG + " DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// 获取屏幕密度(方法3)
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320)
xdpi = dm.xdpi;
ydpi = dm.ydpi;
Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
int screenWidthDip = dm.widthPixels; // 屏幕宽(dip,如:320dip)
int screenHeightDip = dm.heightPixels; // 屏幕宽(dip,如:533dip)
Log.e(TAG + " DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip);
screenWidth = (int)(dm.widthPixels * density + 0.5f); // 屏幕宽(px,如:480px)
screenHeight = (int)(dm.heightPixels * density + 0.5f); // 屏幕高(px,如:800px)
Log.e(TAG + " DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
Components Size( 组件尺寸)
StatusBar
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop=
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;
Log.i("*** Jorgesys::", "StatusBar Height= " + StatusBarHeight + ", TitleBar Height = " + TitleBarHeight);
而如果在Activity的onCreate函数中需要获取Status的高度,则为如下方法:
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
Style & Themes
Theme
- Hoho Theme
在
- Material Design Theme
今年的
compile ‘com.android.support:appcompat-v7:21.0.3’ 中的
View Mechanism
参考资料
Layout
LayoutInflater
参考文章
inflate
如果
attachToRoot 为True
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/custom_button">
</Button>
现在我们想动态地把这个按钮添加进
inflater.inflate(R.layout.custom_button, mLinearLayout, true);
我们指定了用于填充
下面的代码也有同样的效果。
inflater.inflate(R.layout.custom_button, mLinearLayout);
public class MyCustomView extends LinearLayout {
...
private void init() {
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.view_with_merge_tag, this);
}
}
这就是一个很好的使用
attachToRoot 为False
Button button = (Button) inflater.inflate(R.layout.custom_button, mLinearLayout, false);
mLinearLayout.addView(button);
这两行代码与刚才
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(android.R.layout.list_item_recyclerView, parent, false);
return new ViewHolder(view);
}
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(R.id.root_viewGroup);
if (fragment == null) {
fragment = new MainFragment();
fragmentManager.beginTransaction().add(R.id.root_viewGroup, fragment).commit();
}
上面代码中
public View onCreateView(LayoutInflater inflater, ViewGroup parentViewGroup, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout, parentViewGroup, false);
…
return view;
}
具体使用时候要注意:
- 如果可以传入
ViewGroup 作为根元素,那就传入它。但是避免将null 作为根ViewGroup 传入。 - 自定义
View 时很适合将attachToRoot 设置为true ,但是不要在View 已经被添加进ViewGroup 时传入true 。
Widgets
Attribute
ID
有时候在
create folder res/values/ids.xmland
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="refresh" type="id"/>
<item name="settings" type="id"/>
</resources>
in Activity class call like this
ImageView refreshImg = new ImageView(activity);
ImageView settingsImg = new ImageView(activity);
refreshImg.setId(R.id.refresh);
settingsImg .setId(R.id.settings);
Size
常用的控件layout_width、layout_height、width、height这几种。而layout_*这一系列的布局只有用于被layout包裹起来的情况下才会起作用。譬如:
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="80dp"
android:text="Button" >
</Button>