Android TextView无法设置背景颜色或可绘制
不知道以前是否有人遇到过这个问题。我有一个应用程序,它可以检测人脸,并在人脸周围放置可触摸的正方形,所有这些都在相对位置。当触摸时,我想在视图中添加一些文本,这一切都很好,但当我只想在文本视图中添加背景时,它什么也不做。我试过一个标准的背景Android TextView无法设置背景颜色或可绘制,android,textview,setbackground,Android,Textview,Setbackground,不知道以前是否有人遇到过这个问题。我有一个应用程序,它可以检测人脸,并在人脸周围放置可触摸的正方形,所有这些都在相对位置。当触摸时,我想在视图中添加一些文本,这一切都很好,但当我只想在文本视图中添加背景时,它什么也不做。我试过一个标准的背景setBackgroundColor(Color.WHITE)而不是我真正想要使用的背景(setBackgroundResource(R.drawable.nametag);),但仍然没有 TextView nameLabelView = new TextVi
setBackgroundColor(Color.WHITE)代码>而不是我真正想要使用的背景(setBackgroundResource(R.drawable.nametag);
),但仍然没有
TextView nameLabelView = new TextView(activity);
nameLabelView.setText(fullname);
nameLabelView.setTextColor(Color.BLACK);
nameLabelView.setBackgroundColor(Color.WHITE); //TODO <-- wth??
//nameLabelView.setBackgroundResource(R.drawable.nametag);
nameLabelView.setGravity(Gravity.CENTER_HORIZONTAL);
//duplicate layout params from active face View so label sits inside it
ViewGroup.LayoutParams lp = selectedFaceView.getLayoutParams();
nameLabelView.setLayoutParams(lp);
facePreviewLayout.addView(nameLabelView);
TextView nameLabelView=新的TextView(活动);
nameLabelView.setText(全名);
nameLabelView.setTextColor(Color.BLACK);
nameLabelView.setBackgroundColor(颜色:白色)//TODO我已经测试了你的代码,它对我来说运行正常。有必要提供更多信息,以便我能在这方面提供更多帮助。下面的代码工作正常
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView nameLabelView = new TextView(getApplicationContext());
nameLabelView.setText("Test");
nameLabelView.setTextColor(Color.WHITE);
nameLabelView.setBackgroundColor(Color.BLACK); //TODO <-- wth??
//nameLabelView.setBackgroundResource(R.drawable.nametag);
nameLabelView.setGravity(Gravity.CENTER_HORIZONTAL);
//duplicate layout params from active face View so label sits inside it
FrameLayout selectedFaceView = (FrameLayout)findViewById(R.id.frame_layout);
ViewGroup.LayoutParams lp = selectedFaceView.getLayoutParams();
nameLabelView.setLayoutParams(lp);
RelativeLayout facePreviewLayout = (RelativeLayout)findViewById(R.id.relative_layout);
facePreviewLayout.addView(nameLabelView);
}
}
公共类MainActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView nameLabelView=新的TextView(getApplicationContext());
nameLabelView.setText(“测试”);
nameLabelView.setTextColor(Color.WHITE);
nameLabelView.setBackgroundColor(颜色.黑色);//TODO谢谢你janzoner
你的测试真的很感激,很高兴知道我没有发疯。虽然我已经找到了解决方法,但我不知道为什么这个方法有效,我以前的设置没有,也许我盯着它太久了,错过了其他地方的东西
基本上,在之前,我将名字标签TextView
添加到主RelativeLayout
(其子对象是实际图像和所有面部视图
等)
现在我已经把名字标签TextView
移到了它自己的RelativeLayout
中,它位于脸的周围,这就神奇地完成了。这是一种更干净的方法,因为你不能遍历视图的子视图,但你可以遍历RelativeLayout
的子视图,我会的以后需要这个
呸!还有很多改进的余地:)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#00FF00" >
</FrameLayout>
</RelativeLayout>