标签: android-wrap-content

marginTop不适用于ConstraintLayout和wrap_content

在我的Fragment中我有一个ConstraintLayout,layout_height="wrap_content"我希望在视图底部的两个底部之间有一个边距.

当我将这个边距添加layout_marginBottom到上方按钮(button_welcome_signup)时,似乎工作正常.但是,如果我尝试将其添加到底部按钮(button_welcome_signin),因为layout_marginTop它不起作用.

有谁知道这里的问题/如果我做错了什么?

(请注意,我使用wrap_content是有原因的,而且我非常想在底部按钮上使用边距,因此我可以将其添加到其样式中,以便在我的项目中实现更好的UI一致性).

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:MyAppApp="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@color/white"
    android:minHeight="@dimen/min_height_welcome_frame"
    android:padding="@dimen/margin_all_frame_inner">

    <ImageView
        android:id="@+id/imageview_welcome_logo"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/logo_header"
        MyAppApp:layout_constraintTop_toTopOf="parent"
        MyAppApp:layout_constraintLeft_toLeftOf="parent"
        MyAppApp:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/textiew_welcome_title"
        style="@style/MyAppTextViewTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin_all_component_l"
        android:text="@string/welcome_title"
        MyAppApp:layout_constraintTop_toBottomOf="@id/imageview_welcome_logo" />

    <TextView
        android:id="@+id/textview_welcome_text"
        style="@style/MyAppTextViewText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/welcome_message"
        MyAppApp:layout_constraintTop_toBottomOf="@id/textiew_welcome_title" />

    <Button
        android:id="@+id/button_welcome_signin"
        style="@style/MyAppSubButton"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:layout_marginTop="@dimen/margin_all_component_s" 
        android:text="@string/welcome_sign_in"
        MyAppApp:layout_constraintBottom_toBottomOf="parent" />

    <Button
        android:id="@+id/button_welcome_signup"
        style="@style/MyAppButton"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:layout_marginTop="@dimen/margin_all_component_l"
        android:text="@string/welcome_sign_up"
        MyAppApp:layout_constraintBottom_toTopOf="@id/button_welcome_signin"
        MyAppApp:layout_constraintTop_toBottomOf="@id/textview_welcome_text"
        MyAppApp:layout_constraintVertical_bias="1" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

styles.xml:

<style name="MyAppButton" parent="Widget.AppCompat.Button">
    <item …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-xml android-constraintlayout android-wrap-content

15
推荐指数
1
解决办法
1万
查看次数

将内容包装在 Jetpack Compose 中

如何制作父布局 -Box将其内容包装在 Jetpack compose 中?下面当前的实现填充了整个屏幕,我只想Box环绕它的子项 - Switch。如何定义 的换行内容Box

@Composable
fun TestScreen(modifier: Modifier = Modifier) {
    Box(modifier = Modifier.background(Color.Yellow)){
        val switchState = remember { mutableStateOf(true) }
        Switch(
            checked = switchState.value,
            enabled= true,
            onCheckedChange = { switchState.value = it }
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

盒子里的开关

android kotlin android-wrap-content android-jetpack-compose

14
推荐指数
2
解决办法
1万
查看次数

GridLayoutManager - 列宽包装自己最大的子节点

我有一个RecyclerView内部HorizontalScrollView,我希望它使用一个GridLayoutManager.这没关系,但有一件事还是困扰我,每列的宽度是相同的(根据我想的最大单元格宽度?).是否可以包装列的宽度以匹配此特定列的最大单元格?

它应该看起来像这样:

在此输入图像描述

橙色部分是细胞视图所占据的部分.


编辑

我们让我澄清我的期望.一个例子比单词更好,在这里你可以看到一个带有GridLayoutManager的RecyclerView的截图.每个项目都是一个简单的TextView,随机包含10到40个字符之间的文本.如前所述,RecyclerView位于Horizo​​ntalScrollView内.我们可以看到每个列都具有相同的宽度,尽管此列中的任何项都不能满足整个宽度.我想要的是删除那些无用的空白空间和具有不同大小的列,每列与其自己的最大子项的宽度匹配.

在此输入图像描述

如果你想测试这个行为,你可以克隆我在Github上传的这个repo:https://github.com/ShargotthDev/TestGrid

如上所述,这是我的XML布局(非常基本):

<?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">

    <HorizontalScrollView
        android:id="@+id/gameplay_hotizontalScroll_ScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="70dp">

        <android.support.v7.widget.RecyclerView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/recycler_view" />

    </HorizontalScrollView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

编辑2

我应该提到一些单元格的跨度大小可能超过1,而LayoutManager应该是垂直的,这样那些单元格可以在水平而不是垂直方向上占据更多位置(不知道我是否让自己理解).

谢谢你的时间 !

android gridlayoutmanager android-recyclerview android-wrap-content

12
推荐指数
1
解决办法
9860
查看次数

如何将回收器高度设置为 recyclerView 中的最高项目?

我需要确保水平 recyclerView 高度与最大项目的高度相同。

项目可以有不同的高度(项目 = 总是相同的图像 + 标题 + 副标题,标题和副标题可以有无限长度)。当我为我的 recyclerView 设置 wrap_content 时,它会根据可见项目的高度调整大小,这使得 recyclerView 下方的内容跳跃,这是我想要避免的。

我想要达到的目标: 我想要达到的目标 灰色区域是可见视口。

所以基本上我想以某种方式获得最大项目的高度,然后将 recyclerView 高度设置为该数字。

我已经尝试过的是基于标题 + 副标题长度的近似值,但它非常不准确,因为例如,即使两个标题具有相同的文本长度,它们也可能具有不同的宽度,因为我使用的字体不是等宽字体。

android android-recyclerview android-wrap-content

12
推荐指数
3
解决办法
5553
查看次数

在android中使用wrap_content时,可以防止在textview中破坏单词

我正在尝试将桌面应用程序转换为Android - 并且正在努力解决一些非常基本的问题.

当我指定一个布局包括一个包含大量文本的textview时,wrap_content似乎在一个单词的中间任意中断 - 我找不到任何文档表明这可以被控制.

android textview android-wrap-content

6
推荐指数
1
解决办法
3126
查看次数

ExpandableListView可扩展列表的高度为wrap_content,且高度低于它时不扩展

这是我的布局代码。当我单击可扩展列表视图时,它不会扩展。但是,如果我增加列表的高度,它将扩展。有没有办法通过代码动态增加列表的高度?我可以在布局的中间进行扩展吗?因为如果我增加高度,它只会在折叠列表中添加不必要的空白。

码:

<ScrollView 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">

    <RelativeLayout 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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.pocketcash.pocketcash.HomeActivity">

        <Switch
            android:id="@+id/detailedCashInfoSwitch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:onClick="cashToggleSwitchClicked"
            android:text="Toggle Detailed Cash Info" />

        <Button
            android:id="@+id/addCashButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/expandableListView2"
            android:layout_centerHorizontal="true"
            android:onClick="launchAddCash"
            android:text="Add cash" />

        <Button
            android:id="@+id/addExpenditureButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/addCashButton"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="25dp"
            android:onClick="launchAddExpenditure"
            android:text="Add expenditure" />

        <TextView
            android:id="@+id/recentLogsText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/addExpenditureButton"
            android:layout_marginTop="50dp"
            android:text="Recent Logs:"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/logText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/recentLogsText"
            android:layout_marginTop="0dp"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/recentViewAllText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/recentLogsText" …
Run Code Online (Sandbox Code Playgroud)

xml android expandablelistview android-studio android-wrap-content

6
推荐指数
1
解决办法
7443
查看次数

Relativelayout没有包装其内容

我希望我的内心RelativeLayout包装它的内容并保持在View元素之下.为什么它不像下面的图片?

我的layout.xml

<?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">
    <View
        android:id="@+id/linearLayout"
        android:layout_above="@+id/input_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <RelativeLayout
        android:id="@+id/input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#ebebeb">

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_toLeftOf="@+id/imageView13"
            android:layout_toStartOf="@+id/imageView13"
            android:background="@android:color/white"
            android:padding="8dp" />

        <ImageView
            android:id="@+id/imageView13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:src="@drawable/ic_m_send" />

    </RelativeLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

xml android android-layout android-relativelayout android-wrap-content

5
推荐指数
1
解决办法
3061
查看次数

RecyclerView高度wrap_content计算不正确

我想要我RecyclerViewwrap_content.我不希望在RecyclerView中滚动任何内容,它应该调整到内部孩子的高度.我不想让我的父ScrollView滚动我的活动内容.

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <!-- scrolls a little bit as RecyclerView goes slightly down beyond the screen -->
  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
      <!-- still scrolls inside -->
      <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)

填充RecyclerView:

myAdapter = new MyAdapter();
layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(myAdapter);
Run Code Online (Sandbox Code Playgroud)

我使用RecyclerView库来wrap_content解决问题:

dependencies {
    compile 'com.android.support:recyclerview-v7:25.0.0'
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

基本上,RecyclerView高度计算对我来说效果不佳.RecyclerView仍然有它自己的滚动和ScrollView滚动一点.如果我尝试将一些rediculous RecyclerView高度设置为1000dp,使其大于项目总高度,则滚动按需工作,例如RecyclerView不滚动,ScrollView使用所有RecyclerView项目滚动活动.

那我做错了什么?:)

android android-scrollview android-recyclerview android-wrap-content

5
推荐指数
1
解决办法
1127
查看次数

SwipeRefreshLayout中的RecyclerView不是"wrap_content"

我有一个RecyclerView作为唯一的孩子SwipeRefreshLayout,我想要RecyclerView wrap_content.当我将它们都设置为"wrap_content"时,它不起作用.该RecyclerView用的项目也较少match_parent.当我删除SwipeRefreshLayout,RecyclerView意志wrap_content.谁能帮我?我的英语很差,也许你无法理解.有人可以帮帮我吗?非常感谢你.

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

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00ffff">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff00ff">


    </android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)

android swiperefreshlayout android-recyclerview android-wrap-content

5
推荐指数
1
解决办法
3658
查看次数

ViewGroup match_parent 里面的 wrap_content

有一个带有 wrap_content 布局参数的 LinearLayout。我在里面有几个带有 match_parent 参数的视图。让我们来看看。

情况1:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#990000"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="#004400">
    </RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

案例2:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#990000"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:text="What a terrible failure!"
        android:background="#009900"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

案例3:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#990000"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="#000099"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这是预期的行为。视图已绘制。由于声望低于 10,我无法发布第三张图片。感谢堆栈溢出!:)

所有示例均在真实设备上进行测试,而不仅仅是在 android studio 预览中。

从 android Develop -> API Guides -> User Interface -> Layouts: wrap_content - 告诉您的视图将其自身大小调整为其内容所需的尺寸。

考虑第二种情况。我仍然可以理解按钮有内容,而父视图只接受按钮文本和填充——它是“内容”。

但是第一种情况呢?孩子清楚地说“我想和match_parent一起画”。我知道 RelativeLayout 没有孩子,但它不应该打扰父容器还是应该这样做?

或者,如果Android试图通过请求“内容大小”(OnMeasure)来下到较低级别,那为什么我们需要设置宽度。如果它如此聪明并为我决定一切?

这里到底发生了什么?我不明白。为什么会心动?

android android-layout viewgroup android-relativelayout android-wrap-content

5
推荐指数
1
解决办法
1170
查看次数