中国合伙人Android面试

Android面试简录——布局

2015-04-02  本文已影响2779人  nancymi

非技术问题


Android的基本概念

开发Android程序


布局

Android中的布局

布局使用技巧

  1. FrameLayout的主要用途
    FrameLayout主要用于进行层次结构的布局。
  2. 如何实现上、下、左、右、居中对齐?
    android:layout_gravity
  3. 在FrameLayout中后一个View会覆盖前一个View?
    不正确。在FrameLayout中,只有所有的View同样大小,并且在同一个位置上才会被覆盖。类似于PhotoShop,未被覆盖的地方会显示出来。
  1. 如何获得LinearLayout的宽度和高度?
    由于Android的运行机制决定了无法在组件类外部使用getWidth和getHeight。如果想直接获取在布局文件中定义的组件的宽度和高度,可以直接使用View.getLayoutParams().width()和View.getLayoutParams().height。
  2. 如何在多个LinearLayout中添加分隔线?
    适合所有Android版本:在多个LinearLayout放置用于显示分隔线的View。
    Android 3.0 及以上版本:android:showDividers=none/beginning/end/middle.+android:divider(设置一个Drawable ID)
    在Java代码中:linearLayout.setShowDividers|linearLayout.setDividerDrawable.
  3. 如何实现左、右、居中对齐?
    android:orientation="vertical"
    android:layout_weight="1"
    android:layout_width="fill_parent"
  1. 如何确定Button相对于手机屏幕的位置坐标?
    View view = findViewById(R.id.button1);
    int[] locations = new int[2];
    view.getLocationOnScreen(locations);
    int x = location[0];
    int y = location[1];

  2. 如何在Java代码中完成RelativeLayout中设置标签的android:layout_toLeftOf,android:layout_toRightOf功能?
    RelativeLayout relativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.main, null);
    Button button = (Button) getLayoutInflater.inflate(R.layout.button, null);

     RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
    
     layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.button1);
     layoutParams.addRule(RelativeLayout.BELOW, R.id.button1);
    
     button.setParams(layoutParams);
     relativeLayout.addView(button);
    
  3. 如何动态改变RelativeLayout中按钮的布局?
    首先获得按钮对象,然后使用LayoutParams.addRule设置相应的属性。

  1. 请描述一下TableLayout布局的用法。
    TableLayout中嵌套TableRow,TableRow中有不定数目的View,每个View代表一列,每个TableRow代表一行。
  2. <TableLayout>标签中的strechColumns属性的作用是什么?如何使用strechColumns属性?
    用于要拉伸的列的索引。如果指定多个索引列,中间用逗号分隔。
  1. 介绍一下AbsoluteLayout布局的用法。
    绝对布局(坐标布局),View在布局中通过坐标定位。layout_x,layout_y.
  2. 如何动态改变AbsoluteLayout布局中View的位置?
    View view = findViewById(R.id.imageView);
    Layout.LayoutParams layout = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 300, 300);
    view.tParams(LayoutParams);
  1. 如何将当前界面的可视组件以同样的相对位置和大小保存在png图像中?
    View view = getLayoutInflater().inflate(R.layout.main, null);
    View.setDrawingCacheEnabled(true);
    view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasureWidth(), view.getMeasureHeight());
    try {
    Bitmap bitmap = view.getDrawingCache();
    FileOutoutStream fos = new FileOutoutStream("/sdcard/test.png");
    bitmap.compress(CompressFormat.PNG, 100, fos);
    fos.close();
    } catch (Exception e) { }
  2. 如何将Android应用程序窗口的背景色设成渐变色?
    GradientDrawable
    GradientDrawable gradientDrawable = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] {Color.RED, Color.YELLOW});
    getWindow().setBackgroundDrawable(gradientDrawable);

布局属性

高级布局技术

  1. 如何重用布局文件?
    <include>标签引用其他的布局文件,并用android:id属性覆盖被引用布局文件中顶层节点的android:id属性值。
  2. 如何查看apk文件中的布局文件源码呢?
    先将apk文件解压,再使用AXMLPrinter2工具对XML布局文件反编译。
上一篇 下一篇

猜你喜欢

热点阅读