Android面试收集录 Android布局

1.请说出Android中的五种布局,并介绍作用?

  • FrameLayout(堆栈布局),层叠方式显示,类似于PhotoShop上的层叠图层。
  • LinearLayout(线性布局),将视图以水平或者垂直方式显示。
  • RelativeLayout(相对布局),通过确定两个视图的相对位置进行布局。
  • TableLayout(表格布局),将视图以表格的形式排列。
  • AbsoluteLayout(绝对布局),设置View的绝对坐标。
  • GridLayout(网格布局),和TableLayout布局相似。

2.xmlns:android的值可以任意设置吗?

  • xmlns:android是一个命名空间。
  • 它的值需要以 http://schemas.android.com/apk/res开头
  • android是可以替换的,需要自定义一个attrs.xml文件,然后根据配置变量,即可通过命名空间使用。

3.在RelativeLayout中有一个Button,确定相对于手机屏幕的位置坐标?

  View view=findViewById(R.id.button1);

  int[] locations=new int[2];

  view.getLocationOnScreen(locations);

  int x=locations[0];

  int y=locations[1];

4.如何用Java代码设置组件的值?

  • 需要一个LayoutParams对象
  • 通过LayoutParams的一个addRule方法来设置属性的值

5.如何将布局存成图像?

  • view.setDrawingCacheEnabled方法打开图像缓存
  • view.getDrawingCache获取Bitmap对象
  • Bitmap.compress保存图像
  • 注意:首先需要调用measure和layout方法才能成功获取Bitmap对象。

6.设置渐变背景色的方法?

  • 使用GradientDrawable
  • 渐变的图像来渲染窗口背景

7.如何理解android:layout_weight属性?

  • 用于设置组件的重要程度
  • 重要程度越高,值越低

8.对于android:layout_gravity怎么理解的呢?

  • android:layout_gravity属性指定了当前View在父View中的位置
  • android:layout_gravity属性在水平线性布局中时,值为center不起作用

9.如何重用布局文件?

  • 采用include标签
  • <include android:id="@+id/layout1" layout="layout/mylayout">

10.顶层布局使用FrameLayout好不好?

  • 这是多余的,系统会自动生成一个FrameLayout标签
  • 可以用<merge>代替,merge不会生成任何节点

11.如何查看apk文件中的布局文件源代码呢?

  • 反编译工具
  • 使用AXMLPrinter2工具对XML文件反编译
原文地址:https://www.cnblogs.com/Jason-Jan/p/8532687.html