【问题标题】:Is it possible to have a textview slightly out of a cardview in a recyclerview?是否有可能在 recyclerview 中使 textview 稍微偏离 cardview ?
【发布时间】:2019-11-18 05:53:49
【问题描述】:

好的,这似乎是一个简单的问题,但相信我,我已经花费了数小时来完成这个任务。就是想知道cardView的右边能不能稍微带出一个textview。顺便说一句,texview 在许多布局下,例如线性布局和相对布局。

我也尝试在其父布局的 xml 中添加此代码。 android:clipToPadding="false"android:clipChildren="false" 仍然无法正常工作。我不确定为什么它对我不起作用。我附上了我希望 textview 的布局看起来像的图像。我应该在我的 recyclerview 的适配器中添加一些代码吗?请帮忙。提前致谢

  <android.support.v7.widget.CardView xmlns:tools="http://schemas.android.com/tools"
        xmlns:card_View="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_View:cardUseCompatPadding="false">



     <LinearLayout
            android:id="@+id/random"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">




<TextView
        android:id="@+id/random2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>



      <LinearLayout
            android:id="@+id/random3"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">



  <TextView
        android:id="@+id/random4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>



  <View
        android:id="@+id/random5"
        android:layout_width="0dp"
        android:layout_height="0dp"/>



<TextView
        android:id="@+id/this_is_the_textview_i_want_it_to_come_out_slightly"
        android:layout_marginEnd="-5dp"
        android:layout_marginRight="-5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>




        </LinearLayout>
        </LinearLayout>

    </android.support.v7.widget.CardView>

下面附上的代码在cardView里面,cardView会被放到recyclerview中

【问题讨论】:

  • 请把代码复制到这里
  • @alirezadaryani 我已经添加了 xml 的代码。请帮忙。谢谢

标签: android android-recyclerview android-linearlayout android-cardview android-relativelayout


【解决方案1】:

有多种方法可以实现这种效果。我会通过将CardView 封装在另一个ViewGroup 中,然后放入TextView(应该超过CardView 的界限)来做到这一点。

这样一种布局的示例:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
    >

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"

        android:layout_width="match_parent"
        android:layout_height="300dp"

        android:layout_margin="20dp"

        android:foreground="#ACDBDA"

        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </androidx.cardview.widget.CardView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="20% Off"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold"

        android:layout_marginBottom="10dp"
        android:layout_marginEnd="10dp"

        android:padding="7dp"

        android:elevation="5dp"

        android:background="@drawable/temp_rounded_border"

        app:layout_constraintBottom_toBottomOf="@id/cardView"
        app:layout_constraintEnd_toEndOf="parent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

temp_rounded_border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#8080FF" />

    <corners
        android:radius="5dp"
        android:topRightRadius="0dp"
        android:bottomRightRadius="0dp"
        />

    <stroke android:color="#8080FF" android:width="1dp" />

</shape>

我尚未在设备上测试此代码,但这是设计视图在 Android Studio 中呈现的内容。

【讨论】:

  • 嗨@rana,欢迎来到 *,如果您发现答案有帮助,请点赞,因为答案解决了您的问题,我建议您将答案标记为正确/接受,以便当有人else 发现这篇文章他们知道哪个答案最有可能解决他们的问题。
【解决方案2】:

尝试下面的代码并在 cmets 中更新我:

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


<android.support.v7.widget.CardView
    android:clipChildren="false"
    android:clipToPadding="false"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">



    <LinearLayout
        android:id="@+id/random"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">




        <TextView
            android:id="@+id/random2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>



        <LinearLayout
            android:id="@+id/random3"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">



            <TextView
                android:id="@+id/random4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>



            <View
                android:id="@+id/random5"
                android:layout_width="0dp"
                android:layout_height="0dp"/>








        </LinearLayout>
    </LinearLayout>

</android.support.v7.widget.CardView>
<TextView
    android:id="@+id/this_is_the_textview_i_want_it_to_come_out_slightly"
    android:layout_marginEnd="-5dp"
    android:layout_marginRight="-5dp"
    android:layout_marginTop="what ever of your card view"
    android:layout_width="wrap_content"
    android:text="this_is_the_textview_i_want_it_to_come_out_slightly"
    android:layout_height="wrap_content"/>

</FrameLayout>

如果你想在任何地方放任何东西,请使用

框架布局

作为父母

【讨论】:

  • 如果我像你所做的那样放置文本视图,我将无法实现我想要的视图。你有没有看到我添加的图像,我想要实现的视图?
  • 我在代码前添加了图片的链接。