Android开发中遇到问题

本文介绍了Android中关于UI的一些优化技巧,包括如何在代码中设置布局行为,创建可设置最大高度的ScrollView,处理TextView设置粗体后的间距问题,以及如何在DrawerLayout开启时禁用AppBarLayout和CoordinatorLayout的滑动。此外,还讨论了ConstraintLayout中内容超出屏幕的处理和透明背景Dialog的样式设置。

一、Android View

1.1 代码中设置layout_behavior

CoordinatorLayout.LayoutParams lp=(CoordinatorLayout.LayoutParams)getAppBarLayout().getLayoutParams();
lp.setBehavior(new AdaptiveBehavior());

1.2 可设置最大高度AdaptiveHeightScrollView

class AdaptiveHeightScrollView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ScrollView(context, attrs, defStyleAttr) {
    private var mMaxHeight = 0

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.AdaptiveHeightScrollView)
        mMaxHeight=typedArray.getLayoutDimension(R.styleable.AdaptiveHeightScrollView_maxHeight, mMaxHeight)
        typedArray.recycle()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var mHeightMeasureSpec=heightMeasureSpec
        if (mMaxHeight>0){
            mHeightMeasureSpec=MeasureSpec.makeMeasureSpec(mMaxHeight,MeasureSpec.AT_MOST)
        }
        super.onMeasure(widthMeasureSpec, mHeightMeasureSpec)
    }
}

android:scrollbars=“vertical” xml中需要设置,不然滚动条不显示

1.3 TextView设置加粗无效

tab_subtitle.getPaint().setFakeBoldText(true);
 //需要加上下面一句代码
 tab_subtitle.postInvalidate();

1.4 Android 编程中当打开DrawerLayout时禁止AppBarLayout+CoordinatorLayout,反之允许滑动

要实现当 DrawerLayout 打开时禁止 AppBarLayout + CoordinatorLayout 滑动,否则允许滑动,可以这么做:

1.4.1 给 DrawerLayout 添加 DrawerListener:
java
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        // 当抽屉滑动时,禁止 AppBarLayout 和 CoordinatorLayout 滑动
        appBarLayout.setScrollFlags(0);
        coordinatorLayout.setNestedScrollingEnabled(false);
    }

    @Override
    public void onDrawerOpened(View drawerView) {
        // 当抽屉完全打开时,也禁止滑动
    }

    @Override 
    public void onDrawerClosed(View drawerView) {
        // 当抽屉关闭时,恢复 AppBarLayout 和 CoordinatorLayout 的滑动
        appBarLayout.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL  
                | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
        coordinatorLayout.setNestedScrollingEnabled(true);
    }
});
1.4.2 在 onDrawerSlide、onDrawerOpened 方法中调用:
java
AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout);  
appBarLayout.setScrollFlags(0); 

CoordinatorLayout coordinatorLayout = findViewById(R.id.coordinator_layout);
coordinatorLayout.setNestedScrollingEnabled(false);AppBarLayout 的滑动标志设置为 0 以及调用 CoordinatorLayoutsetNestedScrollingEnabled(false) 方法来禁止其滑动。
1.4.3 在 onDrawerClosed 方法中恢复滑动:
java
appBarLayout.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL  
                | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);  
coordinatorLayout.setNestedScrollingEnabled(true);  

1.4.5 TextView设置粗体后有上下间距,怎么避免呢

TextView设置粗体后出现额外的上下间距,这是因为粗体字体本身的font metrics导致的。为避免这个问题,可以通过以下方式:

  1. 在XML中设置android:includeFontPadding=“false”:
xml
<TextView 
    android:text="粗体文本" 
    android:textStyle="bold"
    android:includeFontPadding="false"/>

这个属性的作用是去除字体内衬边距,将可以避免额外的上下间距。
2. 在Java代码中,手动设置Textview的行高:

java
TextView textView = (TextView) findViewById(R.id.text);
textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
textView.setLineSpacing(0, 1); // 设置行高为字体高度的1倍

由于粗体字体高度变高,系统自动增加了行高,导致上下间距变大。我们可以手动设置行高为字体高度的1倍,排除系统自动增加的行高,这样可以避免额外间距。
3. 自定义一个不带内衬的粗体字体:
我们可以找一款开源的无衬线字体(如Roboto),从中提取出粗体字体文件,添加到项目中,然后设置这个自定义的粗体字体,这可以完全避免字体内衬带来的上下间距问题。
// 得到字体文件(Roboto-Bold.ttf)

Typeface customBoldTf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Bold.ttf");  

TextView textView = (TextView) findViewById(R.id.text);  
textView.setTypeface(customBoldTf);

这三种方式可以有效避免TextView设置粗体后出现的上下间距问题。其中第三种方式最为根本和彻底,不需要依赖系统字体,可完全按需定制。但相对也比较繁琐,此情况下前两种简单的方式也已基本足够解决这个小问题。

1.4.6 ConstraintLayout代码中设置约束

 val lp = ConstraintLayout.LayoutParams(
           20,
            20
        )
        lp.leftToLeft=ConstraintLayout.LayoutParams.PARENT_ID
        lp.topToTop=R.id.tv_name
        lp.bottomToBottom=R.id.tv_name
        binding.cvCircle.layoutParams=lp

1.4.7 View.post() 使用

View.post() 原理深入理解

 viewPagerVModel.viewPager.post {
            viewPagerVModel.setCurrentItem(mCurrentSelectPosition)  //设置选中页
        }

1.4.8 CardView设置阴影与底部背景有痕迹,不平滑

如背景为白色

app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="@dimen/dp_5"
app:cardElevation="@dimen/dp_2"                          	  	                                                            

1.4.9 ConstraintLayout 中View组件内容过长超出屏幕问题

 <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp_5">

        <TextView
            android:id="@+id/tvLogisticsCompany"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{data.trackInfo.carriers_title}"
            android:textColor="@color/color_666666"
            android:textSize="@dimen/font_14"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="EWE全球快递" />

        <TextView
            android:id="@+id/tvWaybillNum"
            android:layout_width="@dimen/dp_0"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/dp_10"
            android:layout_marginEnd="@dimen/dp_10"
            android:text="@{data.trackInfo.track_number}"
            android:textColor="@color/color_333333"
            android:textSize="@dimen/font_14"
            android:maxLines="1"
            android:ellipsize="end"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintWidth_default="wrap"
            app:layout_constraintLeft_toRightOf="@+id/tvLogisticsCompany"
            app:layout_constraintRight_toLeftOf="@+id/tvCopy"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="PJVD00001153200" />

        <TextView
            android:id="@+id/tvCopy"
            android:layout_width="@dimen/dp_38"
            android:layout_height="@dimen/dp_19"
            android:layout_marginEnd="@dimen/dp_10"
            android:bind_visibility_boolean="@{Strings.isNotEmpty(data.trackInfo.track_number)}"
            android:background="@{data.copyBtnBg}"
            android:gravity="center"
            android:onClick="@{()->data.copy()}"
            android:text="@string/str_copy"
            android:textColor="@color/color_666666"
            android:textSize="@dimen/font_11"
            app:layout_constraintLeft_toRightOf="@+id/tvWaybillNum"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="复制" />
</androidx.constraintlayout.widget.ConstraintLayout>

在这里插入图片描述
在这里插入图片描述
主要使用以下属性
在这里插入图片描述
android:layout_width=“@dimen/dp_0”
app:layout_constraintHorizontal_chainStyle=“packed”
app:layout_constraintHorizontal_bias=“0”
app:layout_constraintWidth_default=“wrap”

1.5.0 透明背景Dialog样式设置

 <style name="MyDialogStyle" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>

Window window = loadingDialog.getWindow();
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            **lp.dimAmount=0f;**
            window.setGravity(Gravity.CENTER);
            window.setAttributes(lp);
            window.setWindowAnimations(R.style.PopWindowAnimStyle);
            **window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);**

1.6.0 布局

Android ConstraintLayout中TextView组件内容过长超出屏幕问题
https://blog.csdn.net/weixin_41046681/article/details/126250929

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值