动态更改View的大小

如果要更改LinearLayout布局的大小 则可以这样写 

ScreenSizeCaculation 是自己写的一个类  用来计算自己要想转换的像素具体大小。

import android.content.Context;

/**
 * Created by Dai xueyue on 15/4/30.
 */
public class ScreenSizeCaculation {

  public static int dip2px(Context context, float dipValue){
    final float scale = context.getResources().getDisplayMetrics().density;
    return  (int)(dipValue * scale +0.5f);
  }

  public static int px2dip(Context context, float pxValue){
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int)(pxValue / scale + 0.5f);
  }
}

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) styleArea.getLayoutParams();
params.width = LinearLayout.LayoutParams.MATCH_PARENT;
params.height = ScreenSizeCaculation.dip2px(getActivity().getApplicationContext(),300);
styleArea.setLayoutParams(params);


如果是 FrameLayout的话    就改写成 

FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) styleArea.getLayoutParams();
params.width = FrameLayout.LayoutParams.MATCH_PARENT;
params.height = ScreenSizeCaculation.dip2px(getActivity().getApplicationContext(),300);
styleArea.setLayoutParams(params);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 WindowManager 来动态改变 Android 弹窗(悬浮窗)的大小。以下是一种实现方法: 1. 首先,在您的代码中实例化一个 WindowManager 对象: ```java WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); ``` 2. 创建一个布局文件作为您的弹窗的内容,例如 `popup_layout.xml`: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 弹窗内容 --> </LinearLayout> ``` 3. 在您的代码中添加方法来显示和更新弹窗的大小: ```java private void showPopup(int width, int height) { // 创建弹窗视图 View popupView = LayoutInflater.from(this).inflate(R.layout.popup_layout, null); // 设置弹窗的宽度和高度 WindowManager.LayoutParams params = new WindowManager.LayoutParams( width, height, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, // 悬浮窗类型,注意需要适配 Android 8.0 及以上版本 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // 弹窗不获取焦点 PixelFormat.TRANSLUCENT); // 弹窗背景透明 // 显示弹窗 windowManager.addView(popupView, params); } private void updatePopupSize(int width, int height) { View popupView = /* 获取已经显示的弹窗视图 */; WindowManager.LayoutParams params = (WindowManager.LayoutParams) popupView.getLayoutParams(); params.width = width; params.height = height; windowManager.updateViewLayout(popupView, params); } ``` 通过调用 `showPopup()` 方法,您可以创建并显示一个具有指定宽度和高度的弹窗。如果您之后需要更新弹窗的大小,可以调用 `updatePopupSize()` 方法,将新的宽度和高度传递给它。 请注意,显示悬浮窗需要适配 Android 8.0 及以上版本,且需要您的应用拥有悬浮窗权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值