android-custom-view 相关问题

有关在Android平台上构建和使用自定义图形小部件的问题的标记。

绘制圆形矩形描边动画

目前正在深入研究 Canvas 并绘制自定义视图。我刚刚使用此代码创建了一个带边框的圆形矩形(我的 onDraw 方法): 油漆颜色=颜色.红色 油漆.样式 = 油漆.样式.FILL 瓦尔角Ra...

回答 1 投票 0

我想制作一个可以在 Android 中移动文本的 UI

我想制作一个在android中生成卡片新闻的应用程序。 此外,我想制作可以在照片上移动文本的用户界面, 这样我就可以在照片上任意位置设置文字。 怎么会...

回答 1 投票 0

当父级禁用时禁用子视图触摸

我正在创建一个可点击的自定义视图来替换 android.widget.Switch(Switch),使用 onDraw 而不是额外的 Drawable。当我以前使用 Switch 时,如果禁用了父级,则 Switch 也会...

回答 1 投票 0

动态更改 Jetpack Compose 视图大小

我想创建一个具有 3 个预定义尺寸的简单按钮,我在 Compose 视图上添加一个枚举参数,并根据它尝试将预定义尺寸之一设置为宽度和高度。 我的枚举

回答 2 投票 0

在android中创建自定义视图

我对 Android 中的复杂视图不熟悉,我正在尝试创建这种类型的视图示例 1 示例 2。 环顾四周后,我发现可以使用 HTML 和 CSS 来完成。不过,我想...

回答 1 投票 0

如何在android中创建像材质3这样的自定义文本输入布局,因为对我来说现有的材质3和材质2不满足要求

设计师要求我自定义编辑文本或输入文本布局,因此我尝试查看 MD2 和 MD3 文档,但没有找到任何有用的东西。 这是要求,...

回答 1 投票 0

如何动态更改视图的尺寸

我有一个名为 TextCell 的视图,它宽 100sp,高 35sp,还有一个宽 50sp 的垂直线性布局,当我按下按钮时,它会向布局添加 x 数量的 TextCell。 text_cell.xml...

回答 1 投票 0

当我使用自定义视图时,它显示为空白

我想制作一个带边框的网格,所以我创建了一个空单元格视图,它是较大的黑色视图之上的较小的白色视图,因此它看起来像是一个带有黑色边框的白色单元格,但是当我将其放入...

回答 1 投票 0

如何使用自定义视图按钮打开单独的新活动

我的 MainActivity 有一个“添加”按钮,单击该按钮会创建自定义视图的新实例:(https://i.stack.imgur.com/M3Uxe.png) 在它的角落,我的自定义视图有一个图像按钮,应该......

回答 1 投票 0

如何获取我在 XML 中设计的自定义视图并将其用作我的 MainActivity 中的新视图

我设计了我想要的自定义视图: 我设计了我想要的自定义视图的样子: <RelativeLayout> <ConstraintLayout> <ConstraintLayout> <TextView/> <ImageButton/> </ConstraintLayout> <ImageView/> </ConstraintLayout> </RelativeLayout> 如何将其实现到我的 CustomView.java 文件中,以便我可以在 MainActivity.java 和 Activity_layout.xml 中使用它? 我知道我应该使用 LayoutInflater,但所有关于如何以及为什么使用 Kotlin 或印地语的解释我也不明白 当然,我将指导您完成用 Java 实现自定义视图的过程。您是正确的,您可以使用 LayoutInflater 从布局 XML 文件中扩充您的自定义视图。让我们分解一下步骤: 创建您的自定义视图类: 首先创建一个扩展RelativeLayout 并用作自定义视图的Java 类。我们将此类命名为 MyCustomView。 import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; public class MyCustomView extends RelativeLayout { private ConstraintLayout innerConstraintLayout; private ConstraintLayout nestedConstraintLayout; private TextView textView; private ImageButton imageButton; private ImageView imageView; public MyCustomView(Context context) { super(context); init(context); } public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); init(context); } private void init(Context context) { LayoutInflater inflater = LayoutInflater.from(context); inflater.inflate(R.layout.custom_view_layout, this, true); // Initialize your views innerConstraintLayout = findViewById(R.id.innerConstraintLayout); nestedConstraintLayout = findViewById(R.id.nestedConstraintLayout); textView = findViewById(R.id.textView); imageButton = findViewById(R.id.imageButton); imageView = findViewById(R.id.imageView); } // Add methods to interact with your custom views } 创建您的自定义视图布局: 创建一个表示自定义视图的视觉布局的 XML 布局文件。我们将此布局文件命名为custom_view_layout.xml。将此文件放入您的 res/layout 目录中。 <?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="wrap_content"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/innerConstraintLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/nestedConstraintLayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" /> <ImageButton android:id="@+id/imageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher_foreground" /> </androidx.constraintlayout.widget.ConstraintLayout> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher_foreground" /> </androidx.constraintlayout.widget.ConstraintLayout> </RelativeLayout> 在 MainActivity 中使用自定义视图: 现在,您可以在 MainActivity 中使用自定义视图。将自定义视图的实例添加到活动的布局 XML 文件 (activity_layout.xml) 中,或直接以编程方式将其添加到 MainActivity 类中。 MyCustomView customView = new MyCustomView(this); // Set layout params if needed setContentView(customView);

回答 1 投票 0

如何使样式从带有子项的自定义视图类中的布局“继承”?

我有一个名为card.xml 的布局,它是名为Card.kt 的自定义类的“基本”布局。我有一个样式(@style/homepage_block),它适用于基本情况。 卡.xml 我有一个名为 card.xml 的布局,它是名为 Card.kt 的自定义类的“基本”布局。我有一个适合它的风格(@style/homepage_block),它适用于基本情况。 card.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="vertical" android:divider="@drawable/separator_horizontal_empty_10" android:showDividers="middle" style="@style/homepage_block"> <!-- i set orientation and divider --> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/block_title" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/homepage_block_title"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="TITLE" style="@style/text.Header" /> </androidx.constraintlayout.widget.ConstraintLayout> </LinearLayout> 卡.kt package com.tempapplication.views import android.content.Context import android.util.AttributeSet import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.LinearLayout import com.tempapplication.R import com.tempapplication.databinding.CardBinding class Card @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0 ) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) { private val binding: CardBinding init { LayoutInflater.from(context).inflate(R.layout.card, this, true) binding = CardBinding.bind(this) initAttributes(attrs, defStyleAttr, defStyleRes) } // i have a custom attribute called title which i use to set the title text private fun initAttributes(attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) { if (attrs == null) { return } val typedArray = context.obtainStyledAttributes(attrs, R.styleable.Card, defStyleAttr, defStyleRes) binding.title.text = typedArray.getString(R.styleable.Card_title) typedArray.recycle() } override fun addView(child: View?, params: ViewGroup.LayoutParams?) { super.addView(child, params) } } attrs.xml <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Card"> <attr name="title" format="string" /> </declare-styleable> </resources> 所以进一步的问题可以分成两部分。 第一个问题 我可以以某种方式将属性从card.xml“继承”到我使用此布局的自定义视图吗?例如,在 card.xml 中,我将线性布局方向设置为 vertical 并且还有一个自定义分隔线。但是当我在某些活动中包含自定义视图时,它似乎根本不知道它(是它应该是什么样子,以及它实际上是什么样子),所以我必须手动设置分隔符和包含此自定义视图时的方向。 activity.xml <com.tempapplication.views.Card android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:divider="@drawable/separator_horizontal_empty_10" android:showDividers="middle" app:title="PAGE OF WEEK" > <!-- i set orientation and divider even when they are set in card.xml --> <include layout="@layout/content_week" bind:data="@{data.week}"/> </com.tempapplication.views.Card> 也许我需要将分隔线和方向放入style资源中?还是有更优雅的方法来做到这一点? 第二个问题 我有子视图(如上面的代码中包含布局content_week)。如何使根线性布局 (card.xml) 的初始 @style/homepage_block 样式影响我包含在卡片视图中的子项?例如,我希望 Card 的所有子项都有红色背景,因为 card.xml 中使用了样式资源。有可能吗? content_week.xml <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="data" type="com.tempapplication.modules.HomepageBlockWeekData"/> </data> <LinearLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:divider="@drawable/separator_horizontal_empty_10" android:showDividers="middle" style="@style/homepage_block_content"> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:gravity="center_vertical" android:text="@{data.title}" style="@style/text.Header" /> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/_1pxh" android:backgroundTint="#FFCAC4D0" android:background="@drawable/separator" /> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@{data.description}" style="@style/text" /> </LinearLayout> </layout> 您的问题围绕在 Android 中创建自定义视图以及从包含的布局继承样式和属性进行。我将分别解决每个问题: 第一个问题:继承属性 在自定义 Card 视图中,您将根据 card.xml 中定义的自定义属性来扩充 attrs.xml 布局并设置属性。但是,直接在 XML 中为 android:orientation 视图的父布局设置的 android:divider、android:showDividers 和 Card 等属性不会自动应用于自定义视图。 如果您想从父 XML 继承这些属性,您可以通过修改您的 Card 类来实现: class Card @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0 ) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) { // ... (other code remains the same) init { LayoutInflater.from(context).inflate(R.layout.card, this, true) binding = CardBinding.bind(this) // Apply attributes from XML to the custom view val attributes = context.obtainStyledAttributes(attrs, R.styleable.Card, defStyleAttr, defStyleRes) applyAttributes(attributes) attributes.recycle() initAttributes(attrs, defStyleAttr, defStyleRes) } private fun applyAttributes(attrs: TypedArray) { val orientation = attrs.getInt(R.styleable.Card_android_orientation, VERTICAL) val divider = attrs.getDrawable(R.styleable.Card_android_divider) val showDividers = attrs.getInt(R.styleable.Card_android_showDividers, SHOW_DIVIDER_NONE) orientation = VERTICAL // Force vertical orientation for the LinearLayout divider?.let { setDividerDrawable(divider) } setShowDividers(showDividers) } // ... (other code remains the same) } 在此代码中,我添加了一个 applyAttributes 函数,该函数从自定义属性获取的 android:orientation 中读取 android:divider、android:showDividers 和 TypedArray 等属性的值。然后,该函数将这些属性应用到自定义视图,确保继承父 XML 布局中设置的属性。 第二个问题:将样式应用于子视图 如果您希望 Card 自定义视图中的子视图继承 card.xml 布局中定义的样式,则需要为子视图设置适当的样式。在 content_week.xml 中,您可以通过在 style 属性中引用样式来应用样式: <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:gravity="center_vertical" android:text="@{data.title}" style="@style/text.Header" /> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/_1pxh" android:backgroundTint="#FFCAC4D0" android:background="@drawable/separator" style="@style/homepage_block_content_separator" /> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:text="@{data.description}" style="@style/text" /> 在这里,我向子视图添加了 style 属性以应用相关样式。确保在样式资源中定义这些样式(@style/text.Header、@style/homepage_block_content_separator等),并且它们应该应用于子视图。 请记住,设置样式时,您需要确保样式在样式资源中正确定义 (styles.xml),并且它们与您要应用它们的特定视图类型的属性和特性兼容。

回答 1 投票 0

Android中使用抗锯齿绘制Bitmap时纹理被切断的问题如何解决?

我一直在做游戏信息应用。它基本上显示我玩的游戏(和其他许多游戏)的统计数据和信息。这个游戏有动画,我设法制作了自己的绘图视图

回答 0 投票 0

Android 以编程方式使用 AttributeSet 启动自定义视图

嗨,我创建了简单的自定义视图,称为组: 公共类组扩展 LinearLayout { 私有 TextView headerTextView; 公共组(上下文上下文,AttributeSet attrs){ 小...

回答 1 投票 0

禁用和启用视图 android

我的目标是在 google map v2 上绘制自由形状,所以我创建了一个应用程序来在自定义视图中通过手指移动绘制自由形状,并将此视图放在 layout.xml 和 m 中的地图片段上方...

回答 2 投票 0

如何在 Android 中从锚点旋转自定义视图时固定它的位置?

我有一个名为 MyView 的自定义视图,它有两个锚点。我希望能够从它的锚点旋转这个视图,一个锚点作为旋转中心,而用户拖动另一个

回答 0 投票 0

如何将我的自定义视图迁移到 Jetpack Compose?

我有一个以传统方式创建的自定义视图:从 View 类继承。 具体来说,我的自定义视图有许多自定义属性(及其相应的 Kotlin 属性)可以作为...

回答 1 投票 0

视频编辑器时间轴视图 android

我正在尝试创建这种类型的视图 您会注意到视频时间轴移动起来就像视频搜索栏一样,您还可以水平和垂直滚动,这就是我创建的 ...

回答 0 投票 0

在 Android 上缩放时如何防止自定义视图移动?

我正在根据此处的教程在 Android 的自定义视图中实现缩放功能。缩放功能似乎工作正常,但我面临着视图移动的问题......

回答 1 投票 0

Android 自定义 RangeSlider 视图

我正在尝试在下图中创建此视图 这就是我现在创造的 我为自定义视图创建了一个类并扩展了 RangeSlider,这就是我扩展 rangeSlider 的方式 类

回答 0 投票 0

在 Android 中为菜单项使用自定义视图

我在 Android 中使用菜单项的自定义视图时遇到问题,我知道这已经回答了很多次,但我的情况有点独特。 我的菜单只有一个项目,然后有另一个菜单...

回答 0 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.