我有一个使用setContentView(R.layout.activityA)方法设置布局的A活动.activityA布局包含customView.我的customView有一堆setter和getter.如何从A活动中访问它们?当我在acitivity A中创建一个customView实例时,它可以工作,但是customView创建了两次:一次是从setContentView创建的,第二次是我创建它的新实例.有没有其他方法来访问这些方法?请指教.谢谢.
我有一个自定义视图,我想要应用手指画.我该如何实现这一目标?
我自己的观点是210x170像素,我想用手指画画.
我真的很困惑如何做到这一点.
让我们说我有一个自定义RelativeLayout覆盖onMeasure:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(200, 200);
}
Run Code Online (Sandbox Code Playgroud)
现在,我想在此布局中添加一个与此Custom RelativeLayout底部对齐的视图.但我尝试了不同的事情,没有任何结果:
paramsButton.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
addView (mButton, paramsButton);
Run Code Online (Sandbox Code Playgroud)
我的第一个猜测是将底部与父对齐(应该是自定义RelativeLayout对吗?).但显然,它与我的自定义布局的父级对齐(这是另一个RelativeLayout,这次不是自定义.
所以我尝试与id对齐:
paramsButton.addRule(RelativeLayout.ALIGN_BOTTOM, getId());
addView (mButton, paramsButton);
Run Code Online (Sandbox Code Playgroud)
但它也没有工作(按钮消失)注意:我明确地设置我的自定义RelativeLayout的id来检查android确实得到了正确的id.
你知道怎么做这个工作吗?
编辑: 有趣的事情:它适用于以下规则
paramsButton.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
addView (mButton, paramsButton);
Run Code Online (Sandbox Code Playgroud)
所以我不得不说我真的不明白为什么
RelativeLayout.ALIGN_PARENT_BOTTOM
规则不起作用......
这是我的Activity类.我在这里有一个Textview.
我想从View Class设置TextView.
public class TestApp extends Activity
{
TextView NameTxtView;
CustomView view;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.Main);
view = (CustomView)findViewById(R.id.customview);
NameTxtView = (TextView) findViewById(R.id.nameTxtxVw);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的View类.在这里,我想设置TextView文本.我无法在Activity上设置此文本.因为我在View类上获得了价值.
public class CustomView extends View
{
public CustomView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
public void onDraw(Canvas canvas)
{
NameTxtView.settext("Test");
}
}
Run Code Online (Sandbox Code Playgroud)
任何想法怎么做?
谢谢
我正在尝试AlertDialogAndroid中的自定义,偶然发现Exception我不明白.
我AlertDialog在res/layout/dialog_resident_next_dates.xml中定义了自定义
<TextView
android:id="@+id/custom_dialog_title"
style="@style/customdialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="@color/grey3"/>
<TextView
android:id="@+id/custom_dialog_content"
style="@style/customdialog_paragraph"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
在触发的方法中,AlertDialog实现了以下内容
public void onClickResidentDates(View v){
String datesOliva = "a (...) long (...) string";
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setCustomTitle(getLayoutInflater().inflate(R.layout.dialog_resident_next_dates, null));
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
TextView dialogContent = (TextView) findViewById(R.id.custom_dialog_content);
dialogContent.setText(datesOliva);
}
Run Code Online (Sandbox Code Playgroud)
这NullPointerException更准确地导致了在线364
dialogContent.setText(datesOliva);
Run Code Online (Sandbox Code Playgroud)
为什么Exception抛出这个?布局正确充气(使用空Views 测试),TextView应该存在.有没有问题,在这个方法中,Dialog …
java android dialog nullpointerexception android-custom-view
我从图像和其他一些视图创建了一个自定义对话框(不,这不是对话框对象)。我与此自定义对话框(再次是布局)的冲突在于它周围的区域关闭了自定义对话框。有没有办法使外部区域不可点击?
我尝试用全屏frameLayout和透明背景包装对话框视图,然后以编程方式将frame属性设置为setClickable(false)。
framelayout.setClickable(false);
Run Code Online (Sandbox Code Playgroud)
这什么也没做。它仍然关闭对话框。还有其他建议吗?先感谢您。
这是我的代码:
//used to disable background from closing the custom dialog
private FrameLayout fl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.layout_dialog);
btnContinue = (Button) findViewById(R.id.btnContinue);
btnContinue.setOnClickListener(this);
fl.setClickable(false); //background suppose to lock
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnContinue:
finish();
}
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我也有另一个广播类
public class DialogManagerBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(IdeaPlayInterfaceApplication.isActivityVisible()){
Intent i=new Intent(context,CustomDialogActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
} …Run Code Online (Sandbox Code Playgroud) 嗯,这是我在任何地方都找不到的东西。它可能写在某个地方,但我的可能是由于我的搜索技巧不佳而无法找到它。
所以基本上我想要做的是,我想创建一个类,我在其中传递字符串数组,并且从该数组中,该类将返回一个视图,其中按钮的数量作为字符串数组中元素的数量。
就像是,
Public class customView extends View {
public customView(Context context, AttributeSet attrs, String[] array) {
super(context, attrs, array);
}
}
Run Code Online (Sandbox Code Playgroud)
但我无法做到这一点。因为 View 类在构造函数参数中不支持 String 数组。有没有人对此有任何解决方案?我应该采取任何新方法来实现这一目标吗?
谢谢,
杰·斯泰平。
public class ThreadView extends View {
Paint paint = new Paint();
int count;
Handler uiThread = new Handler();
public ThreadView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setColor(Color.BLACK);
paint.setTextSize(80);
uiThread.post(new Runnable() {
@Override
public void run() {
while(true) {
count++;
invalidate();
}
}
});
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawText(count + "", 100, 100, paint);
}
}
Run Code Online (Sandbox Code Playgroud)
我在自定义视图中看到了针对线程的此解决方案,但是没有用。即使我的循环继续调用invalidate(),也不会继续调用,onDraw() 我还看到了一个解决方案,他们在其中实现了Runnable并覆盖了run(),这是一种更好的方法吗?
android ×10
dialog ×2
android-view ×1
autocomplete ×1
canvas ×1
java ×1
paint ×1
textview ×1
toolbar ×1