将屏幕分成两半并将 imageview 放在两半上

Divide screen in two half and place imageview on half-half both

我正在设计一个页面,但此时卡住了。如果有人知道请告诉。 我想这样做:

放置带有圆角正方形的鸟形图像。

您可以在这里找到解决方案:

如果你想calculate the height dynamically你可以使用下面的代码:

int finalHeight, finalWidth;
final ImageView iv = (ImageView)findViewById(R.id.scaled_image);
final TextView tv = (TextView)findViewById(R.id.size_label);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    public boolean onPreDraw() {
        iv.getViewTreeObserver().removeOnPreDrawListener(this);
        finalHeight = iv.getMeasuredHeight();
        finalWidth = iv.getMeasuredWidth();
        tv.setText("Height: " + finalHeight + " Width: " + finalWidth);
        return true;
    }
});

这只是一个想法,如果有帮助的话,您可以将图像分成两部分并将它们与您的布局对齐,如底部对齐和顶部对齐各自的布局

对于圆角背景,您可以在 drawable 文件夹中创建 bg.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <stroke android:width="3dip" android:color="#B1BCBE" />
    <corners android:radius="10dip"/>
    <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>

祝你好运!

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.testing.MainActivity" >

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="#0000ff"
        android:orientation="vertical"
        android:layout_weight="1" />

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="#00ffff"
        android:orientation="vertical"
        android:layout_weight="1" />

</LinearLayout>

<LinearLayout 
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:orientation="vertical"
    android:background="#00ff00"
    android:layout_gravity="center" />

</FrameLayout>

看起来像这样

两年前发过这个帖子,现在看不到有用的答案,所以我自己回答。

可以在 CoordinatorLayout 中使用 layout_anchorlayout_anchorGravity 来完成,我们可以将一个 View 锚定到另一个 View。它非常简单,因为它是 CoordinatorLayout.

的内置行为
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MyActivity">

    <ImageView
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/img_banner" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_logo"
        app:layout_anchor="@id/header"
        app:layout_anchorGravity="bottom|center_horizontal" />

</android.support.design.widget.CoordinatorLayout>

您可以使用布局组件 Guideline 以像素或百分比分割屏幕,并将其用作锚点以将任何其他组件固定到它。