我正在构建一个Android应用程序,我对自定义视图有点挣扎.
我想有一个可重用的视图,它包含一些标准的布局元素.让我们说一个带有一些按钮的relativelayout.
我该怎么办?我应该创建一个扩展RelativeLayout的自定义视图类并以编程方式添加这些按钮吗?我觉得这有点矫枉过正?
在Android中正确执行此操作的方法是什么?
如果我以完全错误的方式解决这个问题,请告诉我,但如果我解释这个问题,我希望有人可以帮助我,或指出我正确的方向.
我需要一个可以移动的自定义视图,我希望在我的一个主要活动中将此视图放在我的relativeLayout的一部分上.我按照移动教程(下面的链接)创建了一个updateThread和一个扩展SurfaceView的类. http://mobile.tutsplus.com/tutorials/android/android-sdk-achieving-movement/
这有效,我可以把它放在我的应用程序中.当我想在我的布局中将其放在不同的活动上时,问题就来了.我设法将视图放入,但它显示全屏,在其他项目后面.我的部分问题是,它的大小取决于屏幕的大小等.nelativelayout使用了大量的图像,Android将选择所需的正确大小的图像.对于SurfaceView,如何设置依赖于手机的大小?
现在,我有相对布局正确显示自定义视图需要去的图像.图像显示正确的大小,只是我需要显示的静态版本.你知道如何将我的自定义视图移动到这个空间吗?
如果你需要任何代码,我可以发布任何代码,我真的很难与这个.即使你可以指出我正确的方向,或者让我知道我是否应该使用表面视图?视图只需要显示一些图像在屏幕上滑动,这就是全部(在主要布局的那一部分内)
非常感谢提前
我有关于处理问题touch events
的CustomView
.I'm添加自定义视图的动态布局(即FrameLayout里).那些具有touchListeners的自定义视图用于在角落处拉点(如下图所示).除此之外我必须在屏幕上拖放整个视图,如果用户触摸除了那些角点之外(图像中的颜色区域)必须拖放视图,否则不会,以及如果用户触摸到外面查看我不想触发任何触摸侦听器.
我可以使用此代码来提取这些点
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (topTouchArea.contains(event.getX(), event.getY())) {
currentTouch = TOUCH_TOP;
} else if (RightTouchArea.contains(event.getX(),event.getY())) {
currentTouch = TOUCH_RIGHT;
} else if (LeftTouchArea.contains(event.getX(),event.getY())) {
currentTouch = TOUCH_LEFT;
} else {
return false; //Return false if user touches none of the corners
}
return true;
case MotionEvent.ACTION_MOVE:
switch (currentTouch) {
case TOUCH_TOP:
top.x = event.getX();
top.y = event.getY();
invalidate();
return true;
case TOUCH_RIGHT:
Right.x = event.getX();
Right.y …
Run Code Online (Sandbox Code Playgroud) 我在另一个答案中读到,在android中,您可以为自定义视图声明具有多种格式的属性,如下所示:
<attr name="textColor" format="reference|color"/>
Run Code Online (Sandbox Code Playgroud)
如何在班上访问这些属性?我应该假设它是一个参考,使用getResources().getColorStateList()
,然后假设它是一个原始的RGB/ARGB颜色,如果Resources.getColorStateList()
抛出Resources.NotFoundException
或有更好的方法来区分格式/类型?
我正在测试一个简单的UIToolbar
,有两个UIBarButtonItem
作为项目,一个使用initWithCustomView:
方法构建,另一个使用initWithTitle:style:target:action:
方法.
第二个按钮可以在工具栏上查看,但第一个按钮丢失了.
问题仅发生在iOS 9和iOS 10上.在iOS 11上不会发生.
有谁有想法吗?
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setTranslatesAutoresizingMaskIntoConstraints:NO];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"one" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:audioOnIcon] forState:UIControlStateNormal];
[toolbar setItems:[NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithCustomView:button],
[[UIBarButtonItem alloc] initWithTitle:@"title" style:UIBarButtonItemStylePlain target:self action:nil],
nil]];
[toolbar setBackgroundImage:[UIImage new] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[toolbar setShadowImage:[UIImage new] forToolbarPosition:UIBarPositionAny];
toolbar.accessibilityIdentifier = toolbarIdentifier;
[self.view insertSubview:toolbar atIndex:[self getSubviewIndex:toolbar.accessibilityIdentifier]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[subview]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:@{ @"subview": toolbar}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[subview]-padding-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:@{ @"padding": @(2 * …
Run Code Online (Sandbox Code Playgroud) 所以我试图创建一个视图,它接受 viewBuilder 内容,循环内容的视图并在每个视图和另一个视图之间添加分隔符
struct BoxWithDividerView<Content: View>: View {
let content: () -> Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
var body: some View {
VStack(alignment: .center, spacing: 0) {
// here
}
.background(Color.black)
.cornerRadius(14)
}
}
Run Code Online (Sandbox Code Playgroud)
所以在我写“here”的地方,如果有意义的话,我想遍历内容的视图。我将编写一个不起作用的代码,但它解释了我想要实现的目标:
ForEach(content.subviews) { view in
view
Divider()
}
Run Code Online (Sandbox Code Playgroud)
怎么做?
在 Flutter 中,创建CustomPainter时,有一个重写方法,shouldRepaint(),您可以返回 true 或 false...大概是为了告诉系统是否重新绘制视图。
\n在文档中,该方法的描述是:
\n\n\nshouldRepaint(covariant CustomPainter oldDelegate) \xe2\x86\x92 bool 每当向 RenderCustomPaint 对象提供\n自定义绘制委托类的新实例时\n或在使用该对象的新实例创建新的\nCustomPaint 对象时调用\n自定义\npainter委托类(这相当于同一件事,因为后者是根据前者实现的)。[...]
\n
除了它返回一个布尔值之外,我基本上不明白任何其他内容。这让我很头疼!我还怀疑深入研究“自定义画家委托类”或“RenderCustomPaint 对象”的定义不会是一种启发性的体验。
\n我很困惑,因为:
\n我认为我们不必担心小部件何时“应该重新绘制”,因为 Flutter 应该根据其自身复杂的优化决策来决定何时何地重新渲染小部件树。
\n我认为 Paint() 方法是您定义“这就是该视图如何绘制自身的方式,(无论何时何地有必要)”的地方
\n我发现的所有示例都只是从该方法返回 false...但我注意到使用 true 与 false 时的不同行为。
\n如果我们总是返回 false,那么它如何重新绘制呢?(即使是假的,它也会重新绘制)
\n如果我们唯一可用的逻辑是将“oldDelegate”与(某物?)进行比较,那么为什么我们需要重写该方法呢?
\n我还没有看到任何示例来演示为什么或如何返回 TRUE,以及为了做出该决定而这样的示例的逻辑是什么样的。
\n一个有知识的人为什么以及如何决定返回 false?
\n一个有知识的人为什么以及如何决定回归真实?
\n谁能像你在和一个 13 岁的孩子(不是 Linus Torvalds)说话一样解释它?
\n一个简单的代码示例和反例会很棒(而不是详尽的明确解释!)
\ncustom-view flutter flutter-custompainter flutter-custompaint
我要在垂直列表视图中创建一个水平列表视图.两个列表视图都可以包含任意数量的元素,并且两者都需要可滚动.
我将如何实现这一点,因为我已经读过android不支持列表视图层次结构.
谢谢 !
我有一个CustomView
来自 的扩展RelativeLayout
。
我CustomView
在布局文件中使用它,但Android Studio在设计模式下不显示CustomView
. 有什么问题吗?
考拉_appbar.xml:
<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="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="top"
tools:context=".activity.basic.BaseActivity">
<ImageView
android:id="@+id/appbar_imgLogo"
android:layout_width="@dimen/appBar_iconWidth"
android:layout_height="@dimen/appBar_iconHeight"
android:src="@drawable/logo"
android:layout_marginTop="@dimen/appBar_iconTopMargin"
android:layout_marginLeft="@dimen/appBar_iconLeftMargin"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/appbar_height"
android:orientation="horizontal"
android:layoutDirection="ltr"
>
<koala.android.customer.views.widget.CustomTextView
android:id="@+id/appBar_txtTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|right"
android:text="@string/app_name_persian"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/TextSize_large"
app:tvCustomFont="@string/baseFont"
/>
<koala.android.customer.views.widget.CustomImageView
android:id="@+id/appBar_imgBack"
android:layout_width="@dimen/appbar_height"
android:layout_height="match_parent"
app:srcCompat="@drawable/ic_arrow_right"
app:ivDrawableTint="@color/colorPrimary"
android:padding="@dimen/appBar_backPadding"
/>
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
KoalaAppBar.java:
public class KoalaAppBar extends RelativeLayout {
private static final String DEFAULT_TITLE = "default";
private static final int DEFAULT_TITLE_COLOR = ResourcesCompat.getColor(AppController.getContext().getResources(), …
Run Code Online (Sandbox Code Playgroud) android custom-view android-custom-view android-layout layout-inflater
macOS 11 Big Sur 在其当前迭代(beta 1 到 beta 6)中有一个错误/功能,这使得很难将 NSMenuItem 与自定义视图一起使用。具体来说,当菜单项突出显示时,项目的自定义视图不会调用 draw(dirtyRect:)。
我设法通过 NSMenu 委托手动调用 draw(dirtyRect:) 方法来绕过该错误:
func menu(_ menu: NSMenu, willHighlight item: NSMenuItem?) {
if #available(OSX 11.0, *) {
// fix for bug when an item with custom view won't be called to draw the highlighting state
menu.items.filter{ $0.tag == 101 }.forEach{ $0.view?.needsDisplay = true }
}
}
Run Code Online (Sandbox Code Playgroud)
但这并不能解开绘制状态的奥秘。MacOS 11 Big Sur 具有新的 UI 外观。菜单项现在以不同的方式突出显示,其内容周围带有圆形框。
我的问题是:我应该手动模拟那个圆形框,还是在新的 App Kit 中有一些默认方式来绘制圆形的菜单项选择?
换句话说,在 macOS 11 Big Sur 中使用自定义视图的 NSmenuItem 的最佳方法是什么?
custom-view ×10
android ×6
appkit ×1
attributes ×1
flutter ×1
ios10 ×1
ios9 ×1
listview ×1
nsmenu ×1
nsmenuitem ×1
surfaceview ×1
swiftui ×1
uitoolbar ×1
view ×1
viewbuilder ×1