当前位置: 代码迷 >> Android >> android 格局中控件居中且向左偏移的写法
  详细解决方案

android 格局中控件居中且向左偏移的写法

热度:63   发布时间:2016-04-28 08:10:02.0
android 布局中控件居中且向左偏移的写法

目标是实现类似如图的效果,居中且向左偏移若干,居中且向右偏移若干


在eoe 提问后大神给出的方案是:


?

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"??? android:layout_width="match_parent"??? android:layout_height="match_parent" >???? <ImageView???? ???android:layout_width="wrap_content"???? ???android:layout_height="wrap_content"???? ???android:layout_gravity="center_horizontal"???? ???android:layout_marginRight="70dp"???? ???android:background="@drawable/red_circle" />???? <ImageView???? ???android:layout_width="wrap_content"???? ???android:layout_height="wrap_content"???? ???android:layout_gravity="center_horizontal"???? ???android:background="@drawable/green_circle" />???? <ImageView???? ???android:layout_width="wrap_content"???? ???android:layout_height="wrap_content"???? ???android:layout_gravity="center_horizontal"???? ???android:layout_marginLeft="70dp"???? ???android:background="@drawable/blue_circle" />?</FrameLayout>

?

?

我自己也琢磨了一种方法:

? ?上面的imageview 使用android:translationX 属性,-70dp表示向左偏移70dp,70dp表示向右偏移70dp

也可以实现同样的效果

?

总结:

第一种方案限定外层使用FrameLayout,直观,明了

第二种方案 不限layout,但是在eclipse预览的时候发现很奇怪的问题,就是虽然view 移动了,但是图形工具里拿鼠标去点那个view的时候发现他还在原来的位置上,不知道这是adt的bug,还是什么原理。但是我有一种预感是,android 界面初始化以后,有一个类似平移动画的动作,这点有待证实。所以最终我还是选择了第一种方案。

  相关解决方案