用9path图片做背景 button和textview的文字不显示

问题在于以下函数:

public void setBackgroundResource (int resid) Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.

也就是说,其实 setBackgroundResource接着调用的应该是:setBackgroundDrawable函数,让我们看看setBackgroundDrawable的描述:

public void setBackgroundDrawable (Drawable d) Since: API Level 1 Set the background to a given Drawable, or remove the background.If the background has padding, this View's padding is set to the background's padding. However, when a background is removed, this View's padding isn't touched. If setting the padding is desired, please use setPadding(int, int, int, int).Parametersd The Drawable to use as the background, or null to remove the background

也就是说,因为9-patch有自己的padding,所以convertView自己的padding被覆盖了!那可咋办呢?

其实知道这个了就很简单了,只要在代码setBackgroundResource之后加上(Remember:一定是这句之后!)一句:

view.setPadding(0, 0, 0, 0);

就可以解决问题了~

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是Android Studio失物招领模块Xml文件的实现代码,请修改代码实现下拉以及图片上传功 <TextView android:id="@+id/shiwuzhaolingzi" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请选择是失物还是招领" /> <Spinner android:id="@+id/spinner_lost_found" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/lost_found_array" /> <TextView android:id="@+id/text_item_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="物品名称" /> <EditText android:id="@+id/edit_item_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/text_item_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="物品描述" /> <EditText android:id="@+id/edit_item_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/text_item_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="时间" /> <EditText android:id="@+id/edit_item_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/text_item_location" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="地点" /> <EditText android:id="@+id/edit_item_location" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/button_upload_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上传图片" /> <TextView android:id="@+id/text_image_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="图片路径:" /> <Button android:id="@+id/button_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交" /> </LinearLayout>
最新发布
05-17
以下是修改后的代码实现下拉和图片上传功能: <TextView android:id="@+id/shiwuzhaolingzi" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请选择是失物还是招领" /> <Spinner android:id="@+id/spinner_lost_found" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/lost_found_array" /> <TextView android:id="@+id/text_item_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="物品名称" /> <EditText android:id="@+id/edit_item_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/text_item_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="物品描述" /> <EditText android:id="@+id/edit_item_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/text_item_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="时间" /> <EditText android:id="@+id/edit_item_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/text_item_location" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="地点" /> <EditText android:id="@+id/edit_item_location" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/button_upload_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上传图片" android:onClick="onUploadImageClick" /> <TextView android:id="@+id/text_image_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="图片路径:" /> <Button android:id="@+id/button_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交" android:onClick="onSubmitClick" /> <!-- 添加下拉框选项数组 --> <string-array name="lost_found_array"> <item>失物</item> <item>招领</item> </string-array> <!-- 添加文件选择器 --> <com.github.dhaval2404.imagepicker.ImagePicker android:id="@+id/imagePicker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 修改后的Java代码: public class MainActivity extends AppCompatActivity { private Spinner mSpinnerLostFound; private EditText mEditItemName; private EditText mEditItemDesc; private EditText mEditItemTime; private EditText mEditItemLocation; private TextView mTextImagePath; private Button mButtonUploadImage; private Button mButtonSubmit; private ImagePicker mImagePicker; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化控件 mSpinnerLostFound = findViewById(R.id.spinner_lost_found); mEditItemName = findViewById(R.id.edit_item_name); mEditItemDesc = findViewById(R.id.edit_item_desc); mEditItemTime = findViewById(R.id.edit_item_time); mEditItemLocation = findViewById(R.id.edit_item_location); mTextImagePath = findViewById(R.id.text_image_path); mButtonUploadImage = findViewById(R.id.button_upload_image); mButtonSubmit = findViewById(R.id.button_submit); mImagePicker = new ImagePicker(this); // 创建文件选择器 // 设置上传图片按钮点击事件 mButtonUploadImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mImagePicker.choosePicture(true /* allow camera */); } }); // 设置提交按钮点击事件 mButtonSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO: 实现数据提交逻辑 } }); } // 在Activity中添加以下两个方法以便与文件选择器交互 @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); mImagePicker.handleActivityResult(requestCode, resultCode, data); } public void onUploadImageClick(View view) { // 显示文件选择器 mImagePicker.choosePicture(true /* allow camera */); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值