Android 实现文字与图片的混排

在我们的项目中,经常会碰到图片与文字混排的问题。解决这类问题的方法有很多,本文给出的方法不是唯一的,只有根据实际场景才能找到更适合的方法。

本文主要通过xml布局来实现图片与文字的混排(水平排列)。


1.利用TextView实现图片与文字混排,

android:drawableBottom在text的下方输出一个drawable,如图片。

如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者。
android:drawableLeft在text的左边输出一个drawable,如图片。
android:drawablePadding设置text与drawable(图片)的间隔,

与drawableLeft、 drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。
android:drawableRight在text的右边输出一个drawable。
android:drawableTop在text的正上方输出一个drawable。


<span style="font-size:18px;">        <TextView
            android:id="@+id/my_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="在线"
            android:textColor="#85898f"
            android:layout_marginTop="5dp"
            android:drawablePadding="5dp"
            android:drawableLeft="@drawable/user_online"/></span>

其中, android:drawablePaddingh很好的解决了图片与文字的间距问题。

2.TextView动态的设置图片

Drawable drawable= context.getResources().getDrawable(R.drawable.text_img);
// 调用setCompoundDrawables时,必须调用Drawable.setBounds()方法,否则图片不显示
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
textView.setCompoundDrawables(drawable, null, null, null); //设置左图标

3.利用RelativeLayout(LinearLayout) 添加 TextView 和 ImageView(ButtonView)来实现

<span style="font-size:18px;">   <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/my_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:background="@drawable/user_online"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            />
        
        <TextView
            android:id="@+id/my_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_toRightOf="@id/my_iv"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            />
    </RelativeLayout></span>


其实也可以通过java代码来实现图片和文字的混排。

   

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现混排,可以使用一个自定义的 EditText 控件,并在其中添加一个 ImageView 控件来实现。 以下是实现方法: 1.创建一个自定义的带有 ImageView 控件的 EditText 控件。 ```java public class RichEditText extends androidx.appcompat.widget.AppCompatEditText { public RichEditText(Context context) { super(context); } public RichEditText(Context context, AttributeSet attrs) { super(context, attrs); } public RichEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } //插入图片 public void insertImage(Bitmap bitmap) { SpannableString ss = new SpannableString(" "); ImageSpan span = new ImageSpan(getContext(), bitmap); ss.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); append(ss); } } ``` 2.在布局件中使用自定义的 EditText 控件。 ```xml <com.example.richedittextdemo.RichEditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` 3.在 Activity 中获取自定义的 EditText 控件,并添加一个点击事件。 ```java public class MainActivity extends AppCompatActivity { private RichEditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = findViewById(R.id.edit_text); editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //调用选择图片的方法 chooseImage(); } }); } //选择图片 private void chooseImage() { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == 1) { try { Uri selectedImage = data.getData(); InputStream inputStream = getContentResolver().openInputStream(selectedImage); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); editText.insertImage(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } } } ``` 4.在 onActivityResult 方法中获取选择的图片,并调用自定义的 EditText 控件的 insertImage 方法,将图片插入到 EditText 中。 以上就是在 Android实现 EditText 插入图片实现混排的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值