记录1

本文介绍了在Android中如何使用两种方式设置TextView文字居中显示的方法,同时涵盖了如何通过监听EditText的文字变化来实现高度自适应调整的技术细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android:lineSpacingExtra="10dp"2种方法可以设置TextView文字居中:

一:在xml文件设置: android:gravity="center"

二:在程序中设置: txtTitle.setGravity(Gravity.CENTER);

设置控件居中:

android:layout_gravity="center"是 对textview控件在整个布局中居中,也可以在其父layout中调用设置 android:gravity="center"

程序中也是需要设置其所在控件的父layout,例如:

RelativeLayout.LayoutParams layoutParams=
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            mTextView.setLayoutParams(layoutParams);

edittext 高度随文字的增加高度自动增加

iProfileEdit.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                changeEditTextHeight(s.toString());
            }
        });



_______________________________________________________________________________________________________________


    private void changeEditTextHeight(String s) {
        TextView textView = new TextView(PostIssueActivity.this);
        textView.setText(s.toString());
        int widthMeasureSpec = MeasureSpec.makeMeasureSpec(iProfileEdit.getMeasuredWidth(), MeasureSpec.EXACTLY);
        int heightMeasureSpec = MeasureSpec.makeMeasureSpec(800, MeasureSpec.AT_MOST);
        textView.measure(widthMeasureSpec, heightMeasureSpec);
        int textHeight = textView.getMeasuredHeight();
        int minHeight = dip2px(80);
        LayoutParams layoutParams = iProfileEdit.getLayoutParams();
        if (textHeight < minHeight) {
            layoutParams.height = minHeight;
        } else {
            layoutParams.height = textHeight;
        }
        iProfileEdit.setLayoutParams(layoutParams);
    }
<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值