将图像与另一个图像的中心对齐

时间:2013-02-01 08:21:05

标签: android android-layout android-xml

我有多个图片需要以特定方式对齐,第二个imageview必须与第一个imageview的中心对齐。如何实现?enter image description here

我附上了一张图片供参考,

截至目前,我正在做的是在obj1的中心保留一个隐藏的视图,并将obj2的顶部对齐,但它似乎不起作用。

3 个答案:

答案 0 :(得分:1)

为什么你不能得到obj3的paddingTop,而其中一半将是obj1的paddingTopmarginTop

答案 1 :(得分:0)

使用RelativeLayout并为ImageViews设置对齐方式:

<RelativeLayout 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:id="@+id/imageViewLeft"
            android:layout_alignParentLeft="true"/>

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageViewRight"
            android:layout_alignParentRight="true"/>
</RelativeLayout>

答案 2 :(得分:0)

你可以自己尝试一下我希望这可以帮助你根据你的要求获得意见 -

创建activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlBase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFF000" >

<ImageView
    android:id="@+id/imageViewRight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/imageViewLeft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:src="@drawable/ic_launcher" />

</RelativeLayout> 

创建imagemiddle.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageViewMiddle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

在主要活动中 -

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

并且 -

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        RelativeLayout rlParent = (RelativeLayout) findViewById(R.id.rlBase);
        int height = rlParent.getMeasuredHeight() / 2;
        Log.d("Height:", height + "");
        rlParent.getLayoutParams().height = height
                + rlParent.getMeasuredHeight();
        rlParent.invalidate();// Change the height of the Relative layout programatically

        // Layout params for the middle image--
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL,
                RelativeLayout.TRUE);
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        ImageView ivMiddle = (ImageView) inflater.inflate(
                R.layout.imagemiddle, null);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
                RelativeLayout.TRUE);
        ivMiddle.setLayoutParams(params);
        rlParent.addView(ivMiddle, params);

    }
}

这不是确切的答案;但这可以帮助您获得所需的输出。

相关问题