我有一个UINavigationBar与自定义UIBarButtonItem(它使用UIButton作为其自定义视图).问题是:自定义按钮的活动区域太大,如果我点击按钮外至少40个像素,它仍然会被注册为按下按钮.这会导致意外敲击.如何减少这些按钮上的活动区域?
iphone uibutton uinavigationcontroller uibarbuttonitem custom-view
我在可可触摸中创建了一个自定义视图,它被UIView超级化,在我的主控制器中我初始化它然后将它作为子视图添加到主视图中,但是当我将它添加到主视图时它再次调用我的初始化方法并导致无限循环.我是否打算创建自定义视图?这是mainView
- (void)loadView {
UIImage* tempImage = [UIImage imageNamed: @"image1.jpg"];
CustomImageContainer *testImage = [[CustomImageContainer alloc] initWithImage: tempImage andLabel: @"test image" onTop: true atX: 10 atY: 10];
[self.view addSubview: testImage];
}
Run Code Online (Sandbox Code Playgroud)
和CustomImageContainer
-(CustomImageContainer *) initWithImage: (UIImage *)imageToAdd andLabel: (NSString *)text onTop: (BOOL) top atX: (int) x_cord atY: (int) y_cord{
UIImageView *imageview_to_add = [[UIImageView alloc] initWithImage: imageToAdd];
imageview_to_add.frame = CGRectMake(0, 0, imageToAdd.size.width, imageToAdd.size.height);
UILabel *label_to_add = [[UILabel alloc] init];
label_to_add.text = text;
label_to_add.alpha = 50;
label_to_add.backgroundColor = [UIColor blackColor];
label_to_add.textColor = [UIColor …Run Code Online (Sandbox Code Playgroud) 对于我的自定义视图,我还定义了一个自定义属性来保持视图的id.它的格式是"参考".
在布局xml中,它定义如下,与android:layout_belowattr 非常相似
<mycustomview id="@+id/cv_1" xyz:nextviewId="@id/cv_2"... />
<mycustomview id="@+id/cv_2" xyz:nextviewId="@id/cv_3"... />
...
<LinearLayout ...>
<mycustomview id="@+id/cv_3" xyz:nextviewId="@id/cv_4"... />
</LinearLayout>
...
Run Code Online (Sandbox Code Playgroud)
它给了我错误我认为这是因为它尚未宣布.
有关访问下一个对象的任何建议类似于这种方法!
我正在考虑使用tag attr为下一个对象找到具有findByTag函数的下一个对象.这是一个很好的方法吗?
非常感谢.
对于自定义视图,这是我不了解的奇怪行为。我在框架布局中有两个视图,一个在另一个视图之上。这些视图很简单,我只是为了简短测试而将它们制成
public class View1 extends View {
....
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
this.updateDrawings();
}
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (canvas != null)
{
Log.i("Test", "Draw View1");
}
}
public void updateDrawings() {
try {
this.invalidate();
}
finally {
}
}
}
Run Code Online (Sandbox Code Playgroud)
和View2
public class View2 extends View {
....
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (canvas != null)
{
Log.i("Test", "Draw View2");
}
}
public void …Run Code Online (Sandbox Code Playgroud) 我试图在自定义中绘制一条线View.在这里,我创建了一个简单Path的只有一个片段,PathShape从中创建了一个,最后将其粘贴到一个中ShapeDrawable,意图使用它在Canvas内部绘制onDraw().但是,这不起作用.在这里看我的例子.
package com.example.test;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.PathShape;
import android.util.Log;
import android.view.View;
public class TestView extends View {
private Path mPath = null;
private Paint mPaint = null;
private PathShape mPathShape = null;
private ShapeDrawable mShapeDrawable = null;
public TestView(Context context) {
super(context);
}
private void init() {
int width = this.getWidth() / 2;
int height = this.getHeight() / …Run Code Online (Sandbox Code Playgroud) 我已经创建了一个UIView的自定义子类以及一个xib文件,并在自定义类中声明了IBOutlets和IBActions.
@interface ContactUsView : UIView
@property (nonatomic, weak) IBOutlet UIButton *displayCloseButton;
- (IBAction)callButtonPressed:(id)sender;
- (IBAction)emailButtonPressed:(id)sender;
- (IBAction)displayCloseButtonPressed:(id)sender;
@end
Run Code Online (Sandbox Code Playgroud)
在xib文件中,我拖入UIView来表示我的自定义视图.我已经设定:
然后我添加了各种按钮,这些按钮与上述3种方法相连.
在ContactUsView.m内部,我有以下内容:
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
NSArray* array = [[NSBundle mainBundle] loadNibNamed:@"ContactUsView" owner:self options:nil];
for (id object in array) {
if ([object isKindOfClass:[ContactUsView class]])
self = (ContactUsView *)object;
}
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
当我创建此视图时,我执行以下操作:
- (void)viewWillAppear:(BOOL)animated
{
ContactUsView *contactUs = [[ContactUsView alloc] initWithFrame:CGRectZero];
CGPoint origin = self.view.frame.origin;
CGSize size = self.view.frame.size;
[contactUs setFrame:CGRectMake(origin.x,
CGRectGetMaxY(self.view.frame) …Run Code Online (Sandbox Code Playgroud) 我实现了可在其中绘画的自定义视图。我想将其设置为MATCH_PARENT,以便它独立于屏幕方向填充整个屏幕。当我将方向更改为横向时,它仅占宽度的50%。
我更改了onMeasure(),但没有效果:
public class DrawScreen extends View{
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(context.getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE){
setMeasuredDimension(screenWidth, screenHeight);
}else{
setMeasuredDimension(screenHeight, screenWidth);
}
}
}
public class MyService extends Service{
...
windowManager.addView(toolbox, params);
}
Run Code Online (Sandbox Code Playgroud) 我是构建Android应用程序的新手。当我在MainActivity中单击按钮时,我想添加一个由按钮和imageView组成的新“自定义视图”。我已经按照下面的一些网站列表来构建自定义视图。
javatechig developer.android.com
他们工作完美。但是,如果我想动态添加自定义视图
sView sview = new sView(MainActivity.this);
Run Code Online (Sandbox Code Playgroud)
以后什么都没发生...
或者,如果我想通过以下方式创建新的自定义视图
sView sview = new sView(MainActivity.this, attrs);
Run Code Online (Sandbox Code Playgroud)
我在哪里可以找到并设置属性?
这是我的代码,在MainActivity中,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDraw = (RelativeLayout) findViewById(R.id.myDraw);
btnAddRect = (Button) findViewById(R.id.btnAdd);
mfView = (sView) findViewById(R.id.draw);
btnAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SingleFingerView sFingerView = new sView(MainActivity.this);
myDraw.addView(sFingerView);
}
});
}
Run Code Online (Sandbox Code Playgroud)
在自定义视图中,
public class sView extends LinearLayout{
public sView(Context context) {
this(context, null, 0);
}
public sView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} …Run Code Online (Sandbox Code Playgroud) 我创建了一个简单的自定义视图,其中包含a RelativeLayout和EditText:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edt_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
此外,我添加了一些自定义属性res/values/attrs.xml,我在自定义视图构造函数中检索这些属性,一切正常.
现在,我想EditText在自定义视图中检索默认属性,例如我想android:text在自定义视图中获取属性.
我的自定义视图类(简化):
public class CustomEditText extends RelativeLayout {
private int panCount;
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.CustomEditText, 0, 0);
try {
this.panCount = typedArray.getInt(R.styleable.CustomEditText_panCount, 16);
} finally {
typedArray.recycle();
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果不重新声明文本属性,我怎么能这样做res/values/attrs.xml呢?
android android-widget custom-view android-custom-view android-attributes
我目前正在尝试了解即将到来的 std::ranges。作为练习,我想从头开始实现一个“toupper”视图,它对某些字符进行范围/视图并将其转换为大写。
将视图和一些迭代器组合在一起非常简单,我不明白如何操作符()和| 必须重载才能像其他范围一样进行组合。
这是我到目前为止想出的“toupper_fn”(遵循范围命名约定)。这几乎是 v3 范围内某些视图函数的复制/粘贴:
struct toupper_fn {
template <typename Rng>
auto operator()(Rng&& rng) const {
return toupper_view{std::forward<Rng>(rng)};
}
template <typename Rng>
friend auto operator|(Rng&& rng, toupper_fn& c)
-> decltype(c(std::forward<Rng>(rng))) {
return c(std::forward<Rng>(rng));
}
template <typename Rng>
friend auto operator|(Rng&& rng, toupper_fn const& c)
-> decltype(c(std::forward<Rng>(rng))) {
return c(std::forward<Rng>(rng));
}
template <typename Rng>
friend auto operator|(Rng&& rng, toupper_fn&& c)
-> decltype(std::move(c)(std::forward<Rng>(rng))) {
return std::move(c)(std::forward<Rng>(rng));
}
};
Run Code Online (Sandbox Code Playgroud)
问题是,对于这些定义,经典函数调用语法 (view(view...)) 和管道语法,仅当我自己的视图是链中的最后一个时才有效。
这是 godbolt 上的完整代码 ->
使用 range-v3: https: //godbolt.org/z/6RlNVC
或 …