Android ViewSwitch自适应显示的view的高度

ViewSwitch继承于FrameLayout,在正常的情况下.他的高度依赖于就是子view中最大高度的view的高度

例如:第一个view高度20dp 第二个高度是100dp 那么ViewSwitch的高度就是100dp

验证代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.gk.testviewswitch.MainActivity">

    <ViewSwitcher
        android:id="@+id/vswitch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:text="Hello World 20dp!" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:text="Hello World 100dp!" />
    </ViewSwitcher>


</LinearLayout>

效果:



那么重点来了,我们如何让他显示哪一个view高度就是该子view的高度呢?

废话不多说.直接上代码:

package com.example.gk.testviewswitch.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ViewSwitcher;

/**
 * 项目名称:Ytb_Android
 * 类描述:
 * 创建人:gk
 * 创建时间:2016/11/28 16:13
 * 修改人:gk
 * 修改时间:2016/11/28 16:13
 * 修改备注:
 */
public class CustomViewSwitch extends ViewSwitcher {
    public CustomViewSwitch(Context context) {
        super(context);
    }

    public CustomViewSwitch(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.e("test", "onMeasure:onMeasure");
        if (getChildAt(0) == getCurrentView()) {
            setDisplayedChild(0);
        } else {
            setDisplayedChild(1);
        }
    }

    @Override
    public void setDisplayedChild(int whichChild) {
        Log.e("test", "whichChild:" + whichChild);
        super.setDisplayedChild(whichChild);
        int viewIndex = whichChild;
        if (viewIndex >= getChildCount()) {
            viewIndex = 0;
        } else if (viewIndex < 0) {
            viewIndex = getChildCount() - 1;
        }
        View child = getChildAt(viewIndex);
        MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayoutParams();
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = layoutParams.height + layoutParams.topMargin + layoutParams.bottomMargin;
        setLayoutParams(params);
    }
}

效果图如下:



完整代码地址:

https://github.com/imgod1/TestViewSwitch

that's all enjoy it

Android Toast 默认的高度是固定的,无法自适应内容的高度。但是,你可以通过自定义布局文件来实现自适应高度的 Toast。 首先,你需要创建一个自定义的 Toast 布局文件,例如命名为 custom_toast.xml: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp" android:background="@drawable/toast_background"> <TextView android:id="@+id/toast_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/white" android:textSize="16sp"/> </LinearLayout> ``` 接下来,在你的代码中,你需要使用 LayoutInflater 加载该布局文件,并将其设置到 Toast 的 View 中。在设置 View 的同时,你还需要设置 Toast 的时长和位置: ``` // 加载自定义布局文件 View toastView = LayoutInflater.from(context).inflate(R.layout.custom_toast, null); // 设置Toast的文本内容 TextView toastText = (TextView) toastView.findViewById(R.id.toast_text); toastText.setText("Toast的文本内容"); // 创建Toast对象 Toast toast = new Toast(context); // 设置Toast的View toast.setView(toastView); // 设置Toast的时长和位置 toast.setDuration(Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); // 显示Toast toast.show(); ``` 最后,你需要注意的是,由于自定义的 Toast 布局文件中的高度是 wrap_content,因此当文本内容过多时,Toast 的高度可能会变得很大,这可能会影响用户体验。因此,你可以考虑在布局文件中添加一些限制条件,例如最大高度或最大行数,以便在保证自适应高度的同时,还能保持 Toast 的合理大小。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值