《第一行代码--Android》阅读笔记之界面设计
1.单位dp、dip、sp、pt、px、in、mm
这里引用StackOverFlow上的一个解答:
- px is one pixel.
- sp is scale-independent pixels.
- dip is Density-independent pixels.( dip == dp is ture)
Here is the difference
dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
即:dp=160*px/dpi
sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.
Remenber this:
You would use
sp
for font sizesdip
for everything else
To make it absolutely clear - try to never use anything but sp or dp unless you absolutely have to. Using sp/dp will make your Android applications compatible with multiple screen densities and resolutions.
2.可见性invisible与gone
invisible:控件消失,点击事件失效,仍然占据着位置
gone:控件消失,布局位置被腾空
3.设置进度条Style
设置为水平:style="?android:attr/progressBarStyleHorizontal"
4.LinearLayout
想要在某一行设置一个EditText和Button, 让Button宽度包裹内容,让EditText占据屏幕剩余宽度
<EditText
android:id="@+id/input_message"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:hint="Type something"
/>
<Button
android:id="@+id/send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Send"
/>
5.一个简易快速ListView,可显示一批String
private String[] data = { "Apple", "Banana", "Orange", "Watermelon",
"Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, data);ListView listView = (ListView) findViewById(R.id.list_view);listView.setAdapter(adapter);
}
6.自定义Adapter时候重载getView()方法的作用
这个方法在每个子项被滚动到屏幕内的时候会被调用
7.ListView性能优化
使用Adapter中getView()方法的参数convertView,避免每次重复加载布局文件
使用ViewHolder来记录绑定的控件id与控件实例
8.编写聊天界面方式选择
使用一个布局,用于存储来往消息气泡,根据消息流向设置控件可见性
这样的方式要优于使用多个布局文件