1、获取标题栏的高度
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT)
.getTop();
int titleBarHeight = contentTop - statusBarHeight;
textView.setText("statusBarHeight" + statusBarHeight + ";contentTop="
+ contentTop + ";titleBarHeight" + titleBarHeight);
2、以Acivity作为布局
注:该类必须继承ActivityGroup
LocalActivityManager activityManager;
activityManager = getLocalActivityManager();
View view2 = activityManager.startActivity("act2",new Intent(this, Act2.class)).getDecorView();
linearLayout.addView(view2, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
3、屏幕截图并保存
View view = getWindow().getDecorView();
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Config.RGB_565);
view.draw(new Canvas(bitmap));
imageView.setImageBitmap(bitmap);
4、android 获取组件尺寸大小
在oncreate()中利用view.getWidth()或是view.getHeiht()来获取view的宽和高。看似没有问题。事实上他们去得值是0。并非你想要的结果?
这是为什么呢?
在调用oncreate()方法时,界面处于不可见状态,内存载入组件还没有绘制出来,你是无法获取他的尺寸。
那怎样在绘制组件之前能获取到该组件的尺寸大小呢?
这里有三种方法,经过验证的:
(1)
int width =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int height =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
view.measure(width,height);
int height=view.getMeasuredHeight();
int width=view.getMeasuredWidth();
(2)
//添加组件绘制之前的监听
ViewTreeObserver vto =view.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public booleanonPreDraw() {
int height =view.getMeasuredHeight();
int width =view.getMeasuredWidth();
}
});
(3)
//添加总体布局监听
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
@Override
public voidonGlobalLayout() {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int height =view.getMeasuredHeight();
int width =view.getMeasuredWidth();
}
});
那么。在activity进入执行期时,组件的尺寸获取方法就非常easy了。直接getWidth()和getHeight().
4、安卓线程池发请求工具类
作用:该类控制程序的请求发送
public class ThreadPoolUtils {
private ThreadPoolUtils() {
}
// 定义核心线程数。并行线程数
private static int CORE_POOL_SIZE = 3;
// 线程池最大线程数:除了正在执行的线程额外保存多少个线程
private static int MAX_POOL_SIZE = 200;
// 额外线程空暇状态生存时间
private static int KEEP_ALIVE_TIME = 5000;
// 堵塞队列。当核心线程队列满了放入的
// 初始化一个大小为10的泛型为Runnable的队列
private static BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(
10);
// 线程工厂,把传递进来的runnable对象生成一个Thread
private static ThreadFactory threadFactory = new ThreadFactory() {
// 原子型的integer变量生成的integer值不会反复
private final AtomicInteger ineger = new AtomicInteger();
@Override
public Thread newThread(Runnable arg0) {
return new Thread(arg0, "MyThreadPool thread:"
+ ineger.getAndIncrement());
}
};
// 当线程池发生异常的时候回调进入
private static RejectedExecutionHandler handler = new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
// 进行重新启动操作
}
};
// 线程池ThreadPoolExecutor java自带的线程池
private static ThreadPoolExecutor threadpool;
// 静态代码块,在类被载入的时候进入
static {
threadpool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE,
KEEP_ALIVE_TIME, TimeUnit.SECONDS, workQueue, threadFactory,
handler);
}
public static void execute(Runnable runnable) {
threadpool.execute(runnable);
}
}
4、动态加入ProgressBar
注意:调用setProgress方法的时候推断假设没有在主线程,会封装一个runnabe然后post到主线程
因此可有看成是在子线程去刷新了组件
progressBar = new ProgressBar(getApplicationContext(), null,
android.R.attr.progressBarStyleHorizontal);
progressBar.setMax(100);
// progressBar.setIndeterminate(false);
// progressBar.setProgressDrawable(getResources().getDrawable(
// android.R.drawable.progress_horizontal));
// progressBar.setIndeterminateDrawable(getResources().getDrawable(
// android.R.drawable.progress_indeterminate_horizontal));
LayoutParams layoutParams2 = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
progressBar.setLayoutParams(layoutParams2);
progressBar.setPadding(5, 5, 5, 5);
runBarLayout.addView(progressBar);
5、抽屉布局的使用
使用抽屉的效果和滑动菜单的差别是,抽屉布局如其如,抽出来后会覆盖以下的布局,而滑动菜单则不会覆盖,而是使其右移。
方法:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ABCDFF"
tools:context="com.example.drawtest.MainActivity" >
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ABFFFF" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</FrameLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="com.drawer.test.LeftMenuFrag"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/leftmenu" />
</android.support.v4.widget.DrawerLayout>