当前位置:网站首页>获取一个控件宽度
获取一个控件宽度
2022-07-22 22:39:00 【Rannki】
在开发中,我们经常会遇到获取某个控件的宽度,但用
view.getWidth()获取到的宽度总是为0,这是因为view视图还没有绘制完成,所以是0,我们需要等它绘制完成后再获取就可以了。所以,上代码:
mainactivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aaaaaaaaaaaaaaaaa"
android:background="@color/black"
android:textColor="@color/white"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/show"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:textColor="@color/black" />
</LinearLayout>MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.text);
// 当textview视图绘制完成后,执行run方法
textView.post(new Runnable() {
@Override
public void run() {
// 获得textview的宽度,单位px
int width = textView.getWidth();
// 显示获取到的width
TextView show = findViewById(R.id.show);
show.setText(width + "px");
}
});
}
}这样,通过用
view.post()来告诉app,让它等这个view绘制完成后,再执行post里面的方法,这个原理呢,是基于【Handler】多线程来完成的。需要注意的是,【view.getWidth()】获取到的宽度,单位是px哟。
效果图:

边栏推荐
- 为什么有的人把代码写的如此复杂?
- 1.10 API and string
- 网站图标的实现
- Using private key to connect to server remotely in pycharm
- networkx对图进行可视化
- 挖财和启牛都是干什么的开户安全吗?
- 21 -- product of arrays other than itself
- How to use selenium.chrome to realize the extended function of intercepting or forwarding requests
- RPC-BDY(5)-服务自动注销、负载均衡
- C language decimal number to binary number
猜你喜欢

深度解析kube-scheduler调度上下文

PostgreSQL database master-slave deployment master database suspended restore master database

【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析

大咖访谈 | 开源社区里各种奇怪的现状——夜天之书陈梓立tison

Dispersion tensor analysis open source software DSI studio simplified Chinese version can be downloaded

Fault tolerant processing with hystrix

开发者分享|MindSpore Lite 体验,一键实现图像分割

第三章 栈

matlab simulink 磷酸铁锂电池仿真

Web资源共享
随机推荐
改变this指向了解一下
networkx对图进行可视化
Redis 持久化操作 (RDB, AOF)
深入浅出地理解STM32中的中断系统——从原理到简单工程示例——保姆级教程
技术干货 | 基于MindSpore详解Perplexity语言模型评价指标
matlab simulink 水能和同步电机发电
大咖訪談 | 開源社區裏各種奇怪的現狀——夜天之書陳梓立tison
Can PHP array subscripts only start from 0
张宇高数30讲总结
JS regular delete the span tag and the content in the tag
怎么使用selenium.chrome实现扩展拦截或转发请求功能
【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析
Typescript对象扩展之对象原型__proto__与prototype
Can PHP split arrays
Matlab保存数据到csv文件的方法分享
开发者分享|MindSpore Lite 体验,一键实现图像分割
RN underlying principle -- 1. Component and purecomponent analysis
Reading notes - > statistics] construction of 12-02 confidence interval -t distribution concept introduction
Easily take you to the gate of turtle drawing
Tensorrt plug-in practice (1)