【问题标题】:Create a clickable item like button with a particuliar view inside创建一个可点击的项目,如按钮,里面有一个特定的视图
【发布时间】:2020-12-17 10:54:36
【问题描述】:

我从 android 开始,但我正在做一个项目,要求我做与下图相同的事情。但是我不知道如何分析这个观点才能产生类似的东西。 从我在活动中看到的内容来看,我有一个“按钮”,当我点击它时,“按钮”中的元素会发生变化。有人可以向我解释如何剪切视图和元素来做同样的事情吗?提前感谢您的回答。

图片:

based view

view after click on "button"

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    我会给你一个粗略的想法..

    它不是一个按钮,它是一个包含 2 个文本视图、2 个图像视图和一个搜索栏的布局,我建议您使用线性布局。在其上添加一个 clicklistner。

    要在单击布局后更改元素,您必须获取其元素 textview 等的引用,并自行更改它们。 ex- seekbar.setprogress(30) 近似为您点击后的图片。

    xml..

    Linearlayout
             textview
              imageview and textview 
              imageview
             seekbar
        \Linearlayout
               
    

    【讨论】:

    • 好的!谢谢你。所以我需要在 xml 中有 10 个这样的布局?
    【解决方案2】:

    @Miina Neko 在卡片视图中使用卡片视图在卡片视图中使用线性布局(或您喜欢的任何其他布局)在下面的代码中使用我为您制作的布局

    <androidx.cardview.widget.CardView
        app:cardCornerRadius="@dimen/_5sdp"
        app:cardElevation="@dimen/_5sdp"
        app:cardBackgroundColor="@color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <LinearLayout
            android:padding="@dimen/_5sdp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="1"/>
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:src="@drawable/refresh_btn"
                    android:layout_width="@dimen/_10sdp"
                    android:layout_height="match_parent" />
                <TextView
                    android:layout_marginLeft="@dimen/_5sdp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="arrrarrr-raaar"/>
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_marginTop="@dimen/_3sdp"
                android:layout_marginBottom="@dimen/_3sdp"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:layout_marginRight="@dimen/_5sdp"
                    android:src="@drawable/refresh_btn"
                    android:layout_width="@dimen/_10sdp"
                    android:layout_height="@dimen/_10sdp" />
                <ImageView
                    android:layout_marginLeft="@dimen/_5sdp"
                    android:src="@drawable/refresh_btn"
                    android:layout_width="@dimen/_10sdp"
                    android:layout_height="@dimen/_10sdp" />
    
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ProgressBar
                    android:layout_marginRight="@dimen/_5sdp"
                    android:layout_marginLeft="@dimen/_5sdp"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
        </LinearLayout>
    </androidx.cardview.widget.CardView>
    

    注意:“@dimen/_sdp”是一个库,我正在共享链接并根据您的选择更改您的可绘制对象,并在 java 或 kotlin 中更改 ID,然后在单击或执行任何操作时以编程方式(如第二张图片)希望回答您题.. SDP库链接https://github.com/intuit/sdp

    【讨论】:

    • 我可以从 xml 为 Activity 的 xml 创建 10 个布局吗?例如,当我们创建一个 listView 时,我们会创建一个示例布局并将其提供给 listView 以创建它们。
    • 我不知道你想要实现什么,但你可以通过 java 创建多个视图,但通过 xml 我认为不可能请你详细说明你想做什么?
    • 没关系,谢谢!我将使用 RecyclerView 和 cardView 就像你给我看的那样。
    • 如果回答有帮助请标记为正确谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 2020-06-11
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多