我想创建您在图片中看到的这种布局,我想像 CSS 中的 negative margin 但没有结果。请帮助我实现它。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".HomeActivity">
<TextView
android:id="@+id/txt_home_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/box"
android:elegantTextHeight="true"
android:padding="30dp"
android:text="Welcome!"
android:textColor="#ffffff"
android:textSize="28sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp">
<ImageView
android:id="@+id/home_logo"
android:layout_width="90dp"
android:layout_height="80dp"
android:scaleType="fitCenter"
android:src="@drawable/mylogo" />
</FrameLayout>
</android.support.constraint.ConstraintLayout>
这是盒子:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#2d3669"></solid>
<corners android:radius="50dp"></corners>
<gradient android:angle="90"
android:startColor="#392800"
android:endColor="#faae03"/>
</shape>
</item>
</selector>
我在 ConstraintLayout
中知道如何让项目彼此靠近,但我无法弄清楚如何将日志放在 TextView
上,以便一半留在外面!我在 Google 上找不到指向此类示例的关键字。
最佳答案
给你:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/txt_home_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/box"
android:elegantTextHeight="true"
android:padding="30dp"
android:text="Welcome!"
android:textColor="#ffffff"
android:textSize="28sp"
tools:layout_editor_absoluteX="129dp"
tools:layout_editor_absoluteY="0dp" />
<android.support.v4.widget.Space
android:id="@+id/space"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toBottomOf="@+id/txt_home_title"
app:layout_constraintLeft_toLeftOf="@id/txt_home_title"
app:layout_constraintRight_toRightOf="@id/txt_home_title" />
<ImageView
android:id="@+id/home_logo"
android:layout_width="98dp"
android:layout_height="84dp"
android:scaleType="fitCenter"
android:src="@mipmap/ic_launcher"
app:layout_constraintHorizontal_bias="0.687"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/space" />
</android.support.constraint.ConstraintLayout>
输出:
解释:
我们不能在 ConstraintLayout
中使用负边距 not officially supported所以我们需要使用变通方法。添加一个空 View ,即 ImageView
和 TextView
之间的 Space
并将 android:layout_marginBottom="32dp"
设置为 空间
View ,你就完成了。
我关注了this CommonsWare 的回答。
关于android - 如何在布局中使用相同的 css "negative margin"技术?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51211591/