Android - draw9patch应该对我有帮助吗? textview

时间:2015-05-30 15:30:54

标签: android view background textview imageview

我需要设置TextView的背景而不像这样伸展:enter image description here

我尝试过draw9patch,但似乎没有做到这一点。我怎样才能达到这个效果?我也可以使用ImageView,但我不知道如何在ImageView中添加文本(我不能使用布局或两个视图......图像和文本需要在一个视图中)

2 个答案:

答案 0 :(得分:0)

您可以使用按钮执行此操作,也不要为其设置任何单击侦听器..: 这是代码:

  Button btnIconTxt=new Button(context);
  btnIconTxt.setId(id);
  //this aligns a image to the left of ur text view(This the important line).
  btnIconTxt.setCompoundDrawablesWithIntrinsicBounds(new BitmapDrawable(context.getResources(),bitmapOfYourImage), null, null, null);

  btnIconTxt.setText(message);
  btnIconTxt.setTextSize(TypedValue.COMPLEX_UNIT_PX, sizeInPx);
  btnIconTxt.setWidth(widthInPx);
  btnIconTxt.setTextColor(Color.RED);

  int currentApi = Build.VERSION.SDK_INT;
  if(currentApi >= 16)
    btnIconTxt.setBackground(btnColor);
  else
    btnIconTxt.setBackgroundDrawable(btnColor);

  RelativeLayout.LayoutParams SDISP_params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  btnIconTxt.setLayoutParams(params1);
  //U can set the x and y margin for this button using 
  //params1.leftMargin = xPixels;
 // params1.topMargin = yPixels;
  yourParentLayout.addView(btnIconTxt);

答案 1 :(得分:0)

我认为你应该使用 drawableLeft 这样的事情:

  <EditText
            android:id="@+id/question_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint=" your question"
            android:drawableLeft="@drawable/ic_menu_info_details"
            android:layout_weight="1"
            android:singleLine="true" />
相关问题