从在UI线程中运行代码的角度来看,之间有什么区别:
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
要么
MainActivity.this.myView.post(new Runnable() {
public void run() {
Log.d("UI thread", "I am the UI thread");
}
});
Run Code Online (Sandbox Code Playgroud)
和
private class BackgroundTask extends AsyncTask<String, Void, Bitmap> {
protected void onPostExecute(Bitmap result) {
Log.d("UI thread", "I am the UI thread");
}
}
Run Code Online (Sandbox Code Playgroud) 这种方法的主要目的是什么setTag()及getTag()的View类型的对象?
我是否正确地认为我可以将任意数量的对象与单个视图相关联?
我有一个TextView,我想沿其顶部和底部边框添加黑色边框.我尝试添加android:drawableTop并android:drawableBottom给TextView的,但只造成了整个视图变黑.
<TextView
android:background="@android:color/green"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableTop="@android:color/black"
android:drawableBottom="@android:color/black"
android:text="la la la" />
Run Code Online (Sandbox Code Playgroud)
有没有办法轻松地在Android中的视图(特别是TextView)中添加顶部和底部边框?
我在for循环中以编程方式添加TextViews并将它们添加到ArrayList.
我该怎么用TextView.setId(int id)?我提出了什么整数ID,所以它不与其他ID冲突?
我正在为回收者视图寻找相当于addHeaderView的东西.基本上我想要一个带有2个按钮的图像作为标题添加到列表视图中.是否有不同的方法将标题视图添加到回收站视图?指导的一个例子会很有帮助
编辑2(添加片段布局):
添加日志语句后,似乎getViewType只接收到0的位置.这导致onCreateView只加载一个布局:
10-26 16:32:53.766 5449-5449/co.testapp I/logger info? Adapter-> getItemCount: 5
10-26 16:32:53.766 5449-5449/co.testapp I/logger info? Adapter-> getItemCount: 5
10-26 16:32:53.766 5449-5449/co.testapp I/logger info? Adapter-> getItemCount: 5
10-26 16:32:53.766 5449-5449/co.testapp I/logger info? Adapter-> getItemCount: 5
10-26 16:32:53.766 5449-5449/co.testapp I/logger info? Adapter-> getItemCount: 5
10-26 16:32:53.766 5449-5449/co.testapp I/logger info? Adapter-> getItemCount: 5
10-26 16:32:53.766 5449-5449/co.testapp I/logger info? Adapter-> getItemCount: 5
10-26 16:32:53.766 5449-5449/co.testapp I/logger info? Adapter-> getItemCount: 5
10-26 16:32:53.766 5449-5449/co.testapp I/logger info? Adapter-> getItemCount: 5
10-26 16:32:53.766 5449-5449/co.testapp I/logger info? …Run Code Online (Sandbox Code Playgroud) 在AppCompat今天发布更新之前,我能够更改Android L中按钮的颜色,但不能更改旧版本.包含新的AppCompat更新后,我无法更改任何一个版本的颜色,当我尝试按钮时就消失了.有谁知道如何更改按钮颜色?
以下图片显示了我想要实现的目标:

白色按钮是默认的,红色按钮是我想要的.
这是我之前做的改变以下按钮颜色的方法styles.xml:
<item name="android:colorButtonNormal">insert color here</item>
Run Code Online (Sandbox Code Playgroud)
并动态地做:
button.getBackground().setColorFilter(getResources().getColor(insert color here), PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)
我也确实将主题父@android:style/Theme.Material.Light.DarkActionBar改为Theme.AppCompat.Light.DarkActionBar
首先:是的,我阅读了关于这个主题的所有其他主题.不仅是来自这个网站的人......(你看,我有点沮丧)
他们中的大多数都提供了使用建议,android:id而不仅仅是id在XML文件中.我做到了.
我从其他人那里了解到,View.findViewById与众不同Activity.findViewById.我也是这样处理的.
在我location_layout.xml,我使用:
<FrameLayout .... >
<some.package.MyCustomView ... />
<LinearLayout ... >
<TextView ...
android:id="@+id/txtLat" />
...
</LinearLayout>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
在我的活动中,我做了:
...
setContentView( R.layout.location_layout );
Run Code Online (Sandbox Code Playgroud)
在我的自定义视图类中:
...
TextView tv = (TextView) findViewById( R.id.txtLat );
Run Code Online (Sandbox Code Playgroud)
返回null.这样做,我的活动工作正常.也许这是因为Activity.findViewById和View.findViewById差异.所以我在本地存储了传递给海关视图构造函数的上下文并尝试:
...
TextView tv = (TextView) ((Activity) context).findViewById( R.id.txtLat );
Run Code Online (Sandbox Code Playgroud)
这也回来了null.
然后,我改变了我的自定义视图扩展ViewGroup,而不是View和改变location_layout.xml让TextView我的自定义视图的直接子,从而使View.findViewById应该工作的设想.惊喜:它没有解决任何问题.
那么我做错了什么?
我会感激任何评论.
我试图以编程方式实现以下(而不是通过XML声明):
<RelativeLayout...>
<TextView ...
android:id="@+id/label1" />
<TextView ...
android:id="@+id/label2"
android:layout_below: "@id/label1" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
换句话说,如何使第二个TextView出现在第一个下面,但我想在代码中执行:
RelativeLayout layout = new RelativeLayout(this);
TextView label1 = new TextView(this);
TextView label2 = new TextView(this);
...
layout.addView(label1);
layout.addView(label2);
setContentView(layout);
Run Code Online (Sandbox Code Playgroud)
谢谢,TreeUK.我理解大方向,但它仍然不起作用 - "B"重叠"A".我究竟做错了什么?
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
Run Code Online (Sandbox Code Playgroud) 在XML文件中,我们可以为视图分配ID android:id="@+id/something",然后调用findViewById(),但在以编程方式创建视图时,如何分配ID?
我认为setId()与默认分配不同.setId()是额外的.
任何人都可以纠正我吗?
android android-view findviewbyid android-resources android-identifiers
我有一个Activity3 EditText秒和一个自定义视图,它作用一个专门的键盘来添加信息到EditTexts.
目前我正在将Activity视图传递给视图,以便我可以获得当前聚焦的编辑文本并更新自定义键盘中的内容.
有没有一种方法可以引用父活动并在EditText不将活动传递到视图的情况下获得当前的焦点?
android ×10
android-view ×10
android-ui ×1
border ×1
button ×1
conflict ×1
findviewbyid ×1
textview ×1
view ×1