gpt4 book ai didi

android - 如何使 View 的宽度依赖于 recyclerview 中的其他 View ?

转载 作者:行者123 更新时间:2023-11-30 00:26:42 24 4
gpt4 key购买 nike

我想测量 cardview 的宽度并与其外部的 cardview 匹配,为了清楚地理解我将附上一张图片和问题:

enter image description here

在这里,在每种情况下,内部 cardview 的宽度应与外部 cardview 的宽度相同,其中内部 cardview 是回复聊天气泡,外部是聊天消息气泡。

我试过扩展卡片 View :

public class ExtendCardView extends CardView {

Integer newWidth;

public ExtendCardView(Context context) {
super(context);
}

public ExtendCardView(Context context, AttributeSet attrs) {
super(context, attrs);
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
newWidth= MeasureSpec.getSize(widthMeasureSpec);
super.onMeasure(newWidth, heightMeasureSpec);

}

public Integer getNewWidth() {
return newWidth;
}

public void setNewWidth(Integer newWidth) {
this.newWidth = newWidth;
}}

在适配器的 onBindView 上,我添加了一个 View 树观察器来更改宽度:

((CardView) holder).inner_card_view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
//((CardView) holder).inner_card_view.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY);

((CardView) holder).inner_card_view.setNewWidth(((CardView) holder).outer_card_view.getNewWidth());
//now we can retrieve the width and height
//...
//do whatever you want with them
//...
//this is an important step not to keep receiving callbacks:
//we should remove this listener
//I use the function to remove it based on the api level!

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
((CardView) holder).inner_card_view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
else
((CardView) holder).inner_card_view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});

什么是实现这样的东西的完美方法,这是我的 cardview 布局文件:

      <com.example.ui.view.ExtendCardView
android:id="@+id/outer_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="8dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="8dp"
android:layout_marginStart="50dp"
app:cardCornerRadius="8dp">


android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp">

<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/black" />

<com.example.ui.view.ExtendCardView
android:id="@+id/inner_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_name"
android:layout_margin="1dp"
android:minWidth="80dp"
app:cardBackgroundColor="@color/red"
app:cardCornerRadius="8dp"
app:cardElevation="0dp">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RelativeLayout
android:id="@+id/rel_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp">

<TextView
android:id="@+id/tv_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textStyle="bold" />

<LinearLayout
android:id="@+id/lin_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_user_name"
android:layout_marginBottom="8dp"
android:orientation="horizontal">

<ImageView
android:id="@+id/iv_type"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:tint="@color/dark_grey"
android:visibility="gone" />

<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web"
android:maxLines="3" />
</LinearLayout>


</RelativeLayout>

<ImageView
android:id="@+id/iv_media"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginEnd="8dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="8dp"
android:layout_marginStart="15dp"
android:visibility="gone" />
</LinearLayout>

</com.nodd.nodd.ui.view.ExtendCardView>

<TextView
android:id="@+id/tv_reply_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/inner_card"
android:layout_marginBottom="5dp"
android:layout_marginEnd="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginStart="4dp"
android:autoLink="web"
android:linksClickable="true"
android:textColor="@color/white"
android:textColorLink="@color/linkedin_blue" />

<ImageView
android:id="@+id/tv_reply_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/inner_card"
android:visibility="gone"
android:layout_marginBottom="5dp"
android:layout_marginEnd="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginStart="4dp"
android:layout_marginTop="5dp" />
</RelativeLayout>

</RelativeLayout>
</com.example.ui.view.ExtendCardView>

最佳答案

看看ConstraintLayout .

ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). It's similar to RelativeLayout in that all views are laid out according to relationships between sibling views and the parent layout, but it's more flexible than RelativeLayout and easier to use with Android Studio's Layout Editor.

您可以将您的内卡放在ConstraintLayout 中,并通过外卡调整其大小,而无需测量和设置尺寸。

由于ConstraintLayout中不允许使用match_parent,因此将layout_height和/或layout_width设置为0dp (调整 layout_margin 属性以增加内卡和外卡之间的空间)。

在你的情况下你会得到类似的东西:

<com.example.ui.view.ExtendCardView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"

android:id="@+id/inner_card"
android:layout_margin="1dp"
android:minWidth="80dp"
app:cardBackgroundColor="@color/red"
app:cardCornerRadius="8dp"
app:cardElevation="0dp">

<!-- ExtendCardView content -->

</com.nodd.nodd.ui.view.ExtendCardView>

希望对您有所帮助!

关于android - 如何使 View 的宽度依赖于 recyclerview 中的其他 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45234422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com