【问题标题】:Responsive layout on AndroidAndroid 上的响应式布局
【发布时间】:2018-02-27 15:09:43
【问题描述】:

我在布局中有三个元素,两个 TextView 和一个 ImageView。我正在尝试实现这种行为:

  1. 图像(箭头)有 9 个补丁
  2. 图像尽可能宽
  3. 图片已设置最小宽度
  4. 左右文本变宽,文本更长,但只是为了满足图像的 minWidth,然后出现新行

以下是来自 Android Studio 布局预览的几张截图,它们是通过不同的布局获得的。

我尝试过的:

  • 相对布局:图片居中,设置最小宽度,文本设置为 toRightOf/toLeftOf
  • 相对布局:图像已设置为 toRightOf/toLeftOf
  • 约束布局:“约束箭头”从图像转到文本
  • 约束布局:“约束箭头”从文本到图像
  • 线性布局:使用权重(不是真正响应式)

我总是得到什么:

我想要的东西对我来说似乎很简单,但我做不到。我错过了什么还是真的不可能?提前谢谢你。

【问题讨论】:

  • 你试过的代码在哪里
  • 嗯,我正在考虑附加一些代码,但我尝试了很多可能性 - 很难选择一个或全部。但是,如果你寻找答案,你可以看到 2/5 的主要尝试 :-) 我的问题不是“这段代码有什么问题”,而是“如何实现这个”。
  • 你好兄弟,我找到了你可能喜欢的东西。 FlexBoxLayout TutorialFlexBoxLayout Github Page.

标签: android android-layout android-relativelayout android-constraintlayout


【解决方案1】:

使用下面的代码

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

    <TextView
        android:id="@+id/left_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="TextView" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="100dp"
        android:layout_toLeftOf="@+id/right_tv"
        android:layout_toRightOf="@+id/left_tv"
        app:srcCompat="@drawable/arrow" />

    <TextView
        android:id="@+id/right_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="TextView" />
</RelativeLayout>

arrow替换你的图片

【讨论】:

  • 这是列表“我尝试过的”中的第二点。这正是图像上的“我总是得到的”。
  • @JerabekJakub 将 maxWidth 设置为 TextView 和一些 dp,例如 android:maxWidth="100dp"
  • 这有点帮助,但我不会让布局响应。
  • @JerabekJakub 你所说的响应是什么意思?如果您想说横向和便携的宽度不同,则仅将 minimumwidth 设置为 imageview。并从两个文本视图中删除 maxwidth
  • @JerabekJakub 喜欢使用 'android:minWidth="100dp"` 到 ImageView 并从两个文本视图中删除 android:maxWidth="100dp"。这将有助于在两个方向上获得可用宽度
【解决方案2】:

这是通过约束布局通过创建水平链完成的另一个答案。

按您的要求更新

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/left_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="ajn sdhbch chf nbhjgvbf vkjf vhf bvihfijnvj vfknvv ihvb jnvkjnfv kjnvfn"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/imageView6"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/left_tv"
        app:layout_constraintRight_toLeftOf="@+id/right_tv"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_next_button" />

    <TextView
        android:id="@+id/right_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/imageView6"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

【讨论】:

  • 看起来很有希望,但结果是这样的:imgur.com/a/5WPoO 长文本没有被分成更多的行并且相互溢出。
  • 然后将 textview layout_height 更改为 layout_height="0dp",即匹配父大小。结果是这样的。 imgur.com/QpIomI5
  • 请更新您的答案,只有这个更改对我不起作用。
  • 已通过将layout_width 设为0dp 来更新答案,抱歉,它的layout_width 不是我之前提到的layout_height
  • 谢谢。我认为这个解决方案是迄今为止最好的。但这仍然不是我想要的 - 当文本很短时,图像不会被缩放。这就是为什么我不会接受你的回答的原因。但无论如何,谢谢你,我至少赞成你的回答。
【解决方案3】:

更新:代码已更新,布局更灵敏。

现在在两个文本视图中使用maxwidth

对于您的问题,以编程方式更新 maxwidth 会更好。

这段代码是用线性布局方法完成的。还有一个GridLayout 方法和constraintLayout 方法。如果您知道正确的技术,可以通过多种布局来实现。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <TextView
        android:id="@+id/left_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView is long and very long" />
    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/ic_add_circle_green" />
    <TextView
        android:id="@+id/right_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="60dp"
        android:text="TextView is long and very long" />
</LinearLayout>

【讨论】:

  • 这是“我尝试过的”列表中的第五点。这使得左右列的大小相同,这与世界“响应式”完全相反。
  • 不,还是一样。当您使用权重时,这些元素仍将具有相同的宽度。我需要他们灵活处理短/长文本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 2013-05-27
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
  • 2013-06-30
  • 1970-01-01
相关资源
最近更新 更多