Android RelativeLayout和ImageView以及自动半页边距

Android RelativeLayout和ImageView以及自动半页边距,android,android-imageview,android-relativelayout,margin,Android,Android Imageview,Android Relativelayout,Margin,我想在我的Android应用程序中复制以下UI,但我有几个问题 我使用了一个RelativeLayout 每个圆都是一个ImageView 负边距用于圆圈左和圆圈右 我的尝试: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_pare

我想在我的Android应用程序中复制以下UI,但我有几个问题

  • 我使用了一个
    RelativeLayout
  • 每个圆都是一个
    ImageView
  • 负边距用于圆圈左和圆圈右

我的尝试:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/CircleCenter"
        android:layout_marginTop="120dp"
        android:background="@drawable/CircleCenter"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/CircleLeft"
        android:background="@drawable/CircleLeft"
        android:layout_alignTop="@+id/CircleCenter"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="false"
        android:layout_marginLeft="-70dp"
        android:layout_marginTop="-20dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/CircleRight"
        android:background="@drawable/CircleRight"
        android:layout_alignTop="@+id/CircleCenter"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="false"
        android:layout_alignParentStart="false"
        android:layout_marginRight="-70dp"
        android:layout_marginTop="-20dp" />

</RelativeLayout>

问题

我认为这没那么糟糕,但有一个问题:如何拥有自动负边距(使用类似于:marginRight=-(ImageView-width/2)的东西)

谢谢!

以编程方式:

RelativeLayout.LayoutParams paramsLeft = (RelativeLayout.LayoutParams) mCircleLeft.getLayoutParams();
paramsLeft.setMargins(mCircleLeft.getMeasuredWidth()/2, marginTop, 0, 0);
// do the same thing for the right image
编辑:如果执行代码时
getMeasuredWidth()
is==0,请添加ViewTreeObserver,他将在渲染视图时告诉您:

ViewTreeObserver vto = mCircleLeft.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {

     // execute the previous code here

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
           obs.removeOnGlobalLayoutListener(this);
      } else {
           obs.removeGlobalOnLayoutListener(this);
      }

    }
});
解决方案#1(基于@Andros)

我们需要通过编程来实现这一点

注意:在系统不渲染视图之前(通常从onCreate/onResume开始),不能在视图上使用width/height/getMeasuredWidth/getMeasuredHeight。因此,我们需要使用
ViewTreeObserver

查看此示例(我使用RoboGuice简化代码):

它并不完美,因为我必须为imageViewRight创建一个新的ViewTreeObserver

也许有一种更有效的方法可以做到这一点?


编辑:

解决方案#2

我们可以使用
.post(new Runnable()
),而不是使用
ViewTreeObserver


在视图膨胀中以编程方式执行该逻辑(marginRight=-(ImageView width/2))?如果你不能在XML中执行此类操作,那么你必须以编程方式执行该操作太糟糕了!你知道我们如何以编程方式执行该操作吗?的确,imageViewLeft。
getMeasuredWidth()
返回0。可能是因为
layout\u width=“wrap_content”
layout_width=“wrap_content”
?什么是
mWebview
?很抱歉出现“mWebview”,复制粘贴错误…我编辑了我的答案。它应该可以与ViewTreeObserver一起使用。在编辑我的问题时,对您的代码进行了一些调整。但我验证了您的答案。谢谢!
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;

@ContentView(R.layout.activity_foo)
public class FooActivity extends Activity {

    @InjectView(R.id.CircleLeft)
    private ImageView circleLeft;

    @InjectView(R.id.CircleCenter)
    private ImageView circleCenter;

    @InjectView(R.id.CircleRight)
    private ImageView circleRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ViewTreeObserver vto = circleLeft.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                RelativeLayout.LayoutParams paramsLeft = (RelativeLayout.LayoutParams) circleLeft.getLayoutParams();
                paramsLeft.setMargins(-circleLeft.getMeasuredWidth()/2, -40, 0, 0);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    circleLeft.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    circleLeft.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            }
        });

    }

}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    cicleLeft.post(new Runnable() {
        @Override
        public void run() {
            RelativeLayout.LayoutParams paramsLeft = (RelativeLayout.LayoutParams) cicleLeft.getLayoutParams();
            paramsLeft.setMargins(-cicleLeft.getMeasuredWidth()/2, -40, 0, 0);
        }
    });
}