android - ConstraintLayout 视图大小响应
问题描述
我正在尝试使用ConstraintLayout
as进行布局,parent
其中有三个buttons
如下图所示。
xml 布局文件如下所示:
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Button
android:id="@+id/splash_facebook_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="15dp"/>
<Button
android:id="@+id/splash_sign_in_btn"
android:layout_width="180dp"
android:layout_height="45dp"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/splash_facebook_btn" />
<Button
android:id="@+id/splash_sign_up_btn"
android:layout_width="180dp"
android:layout_height="45dp"
android:layout_marginEnd="15dp"
app:layout_constraintBottom_toBottomOf="@id/splash_sign_in_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.995"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="@id/splash_sign_in_btn"
app:layout_constraintTop_toTopOf="@id/splash_sign_in_btn"
app:layout_constraintVertical_bias="0.0" />
我想这样做而不为底部的两个按钮设置固定值。
我知道我可以通过更改为LinearLayout
和使用来实现这一点,layout_weight
但我想在有ConstraintLayout
父母的情况下做到这一点。
有没有办法做到这一点?
解决方案
对于左键设置android:layout_marginEnd="8dp"
为右android:layout_marginStart="8dp"
,然后以编程方式设置它们的宽度:
public int getScreenWidth() {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size.x/2;
}
这样你会得到screen width/2
,然后你可以用这个值设置按钮的宽度。
如果你只喜欢 XML,试试这个:
<?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">
<Button
android:id="@+id/splash_facebook_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="15dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginTop="8dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@+id/splash_facebook_btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>
<Button
android:id="@+id/splash_sign_in_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="8dp"
android:layout_weight="1" />
<Button
android:id="@+id/splash_sign_up_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
推荐阅读
- javascript - 我如何在java脚本中创建一个对象数组
- python - 为什么 Scrapy.response.follow() 在迭代中随机抓取链接列表?因为递归?如何解决?
- angular - 网站上的Angular项目刷新结果404页面
- flutter - 点击屏幕时调试控制台中的“opservice is null false”消息
- python - 检查它是否是Python中任何组的子集的有效方法
- python - PYTHON-记录到文件并将 DEBUG 语句打印到控制台
- c# - 使用 C# 在 Linux 中读取锁定文件
- javascript - d3.js hexbin - 适应阈值比例
- javascript - 是否存在纯文本无头 CMS(或类似服务)?
- r - renderUI 基于表中的行