如何将imageviews一半放在parentlayout上,一半放在childlayout上

时间:2016-11-14 12:38:39

标签: android imageview

如何将imageviewhalf放在parentlayout上并将一半放在childlayout上

xml代码位于

之下
  

我尝试过relativelayout和framelayout   请参阅链接image link

中的图片
    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/parent"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@color/colorPrimary">

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/child"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/parent">

    </RelativeLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:src="@drawable/school" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:src="@drawable/school" />

    </FrameLayout>
</RelativeLayout>
  

我有addede链接以澄清要求

2 个答案:

答案 0 :(得分:0)

Please check this code 

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

    <RelativeLayout
        android:id="@+id/parent"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@color/colorPrimary"></RelativeLayout>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_margin="30dp"
        android:layout_toLeftOf="@id/imageView2"
        android:src="@drawable/kidwithphone" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="14dp"
        android:src="@drawable/fab_blue_one"
        android:layout_alignTop="@+id/imageView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="14dp"
        android:layout_marginTop="87dp" />

</RelativeLayout>

答案 1 :(得分:0)

您可以通过编程方式执行此操作,使其适用于任何屏幕尺寸,图像和/或布局尺寸。

首先,在创建适当的@dimen值后,更改为:

android:layout_height="150dp"

为:

android:layout_height="@dimen/top_layout_height"

另外,在你的frameLayout中添加一个id:

android:id="@+id/my_frame"

最后,将它放在你的onCreate方法中:

//Get the image and topLayout height, as we'll need them later 
        int imageHeight = ResourcesCompat.getDrawable(getResources(),
                                                R.drawable.school,
                                                null).getMinimumHeight();
        int topLayoutHeight = (int) getResources().getDimension(R.dimen.top_layout_height);

        //Get a reference to the FrameLayout then get its LayoutParameters
        FrameLayout myFrameLayout = (FrameLayout) findViewById(R.id.my_frame);
        RelativeLayout.LayoutParams params =
                (RelativeLayout.LayoutParams) myFrameLayout.getLayoutParams();

        //Set the top margin so that the FrameLayout is always in the middle
        params.setMargins(0, topLayoutHeight - imageHeight/2, 0, 0);

        //Apply the new parameters
        myFrameLayout.setLayoutParams(params);

这里是result

相关问题