在Android的API中可以发现有很多用整数集来作为参数的地方,先来看一下实例。 LinearLayout是大家所熟知的一个UI基本元素,它里面有一个方向的属性,可以通过以下方法来设置: LinearLayout.setOrientation(int); 使用的时候,通常都是这样: LinearLayout.s
在Android的API中可以发现有很多用整数集来作为参数的地方,先来看一下实例。LinearLayout.setOrientation(int);
LinearLayout.setOrientation(LinearLayout.HORIZONTAL); LinearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.setOrientation(0); // LinearLayout.HORIZONTAL = 0 LinearLayout.setOrientation(1); // LinearLayout.VERTICAL = 0x01
LinearLayout.setOrientation(Integer.MAX_VALUE); LinearLayout.setOrientation(Integer.MIN_VALUE); LinearLayout.setOrientation(2012);
LinearLayout.setOrientation(0); LinearLayout.setOrientation(1);
<LinearLayout android:orientation="vertical" android:gravity="center">
因为xml布局会在编译时被处理,如果有非法的值,会有编译错误的。我想这也就是android特别鼓励开发者用xml来制作所有的布局的一个原因吧。
实例,三个没有设置指向的线性布局,默认是水平放置,在代码中设置了几个离谱的值,发现它们还是水平的,也就是说设置离谱的值不会出错,但也不起作用:
运行结果如下:
代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" > <LinearLayout android:id="@+id/linearlayout_test_1" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff00ff00" android:background="#aa331155" android:layout_weight="1" android:textSize="18sp" android:text="Microsoft" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffff0000" android:background="#aa117711" android:layout_weight="1" android:textSize="18sp" android:text="Apple" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff0000ff" android:background="#aa774411" android:layout_weight="1" android:textSize="18sp" android:text="Google" /> </LinearLayout> <LinearLayout android:id="@+id/linearlayout_test_2" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff00ff00" android:background="#aa331155" android:layout_weight="1" android:textSize="18sp" android:text="Microsoft" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffff0000" android:background="#aa117711" android:layout_weight="1" android:textSize="18sp" android:text="Apple" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff0000ff" android:background="#aa774411" android:layout_weight="1" android:textSize="18sp" android:text="Google" /> </LinearLayout> <LinearLayout android:id="@+id/linearlayout_test_3" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff00ff00" android:background="#aa331155" android:layout_weight="1" android:textSize="18sp" android:text="Microsoft" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffff0000" android:background="#aa117711" android:layout_weight="1" android:textSize="18sp" android:text="Apple" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff0000ff" android:background="#aa774411" android:layout_weight="1" android:textSize="18sp" android:text="Google" /> </LinearLayout> </LinearLayout>
package com.android.explorer; import android.app.Activity; import android.os.Bundle; import android.widget.LinearLayout; public class LinearLayoutTest extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.linearlayout_test); LinearLayout one = (LinearLayout) findViewById(R.id.linearlayout_test_1); one.setOrientation(2012); LinearLayout two = (LinearLayout) findViewById(R.id.linearlayout_test_2); two.setOrientation(Integer.MAX_VALUE); LinearLayout three = (LinearLayout) findViewById(R.id.linearlayout_test_3); three.setOrientation(Integer.MIN_VALUE); } }
public class LinearLayout extends ViewGroup { private Orientation mOrientation; public enum Orientation { HORIZONTAL, VERTICAL }; public void setOrientation(Orientation dir) { mOrientation = dir; } }
import android.widget.LinearLayout; LinearLayout.setOrientation(Orientation.HORIZONTAL); LinearLayout.setOrientation(Orientation.VERTICAL);
Button.setEnabled(true); // enable the button Button.setEnabled(false); // disable the button
// com/android/mms/data/ContactList.java public String[] getNumbers(boolean);
String[] mms = getNumbers(true); String[] sms = getNumbers(false);
Button.setEnabled(true); // enable the button Button.setEnabled(false); // disable the button
Button.enable(); Button.disable();
// com/android/mms/data/ContactList.java public String[] getNumbersForSms(); public String[] getNumbersForMms();
// com/android/mms/data/ContactList.java public String[] getNumbersForSms() { return getNumbers(false); } public String[] getNumbersForMms() { return getNumbers(true); } private String[] getNumbers(boolean) { // implementation }
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号