当前位置:网站首页>Get a control width
Get a control width
2022-07-23 08:20:00 【Rannki】
In development , We often encounter getting the width of a control , But with
view.getWidth()The obtained width is always 0, This is because view The view has not been drawn yet , So it is 0, We need to get it after it is drawn . therefore , Code up :
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);
// When textview After drawing the view , perform run Method
textView.post(new Runnable() {
@Override
public void run() {
// get textview Width , Company px
int width = textView.getWidth();
// Display the acquired width
TextView show = findViewById(R.id.show);
show.setText(width + "px");
}
});
}
}such , By using
view.post()To tell app, Let it wait for this view After drawing , Re execution post The method inside , This principle , Is based on 【Handler】 Multithreading . It should be noted that ,【view.getWidth()】 Obtained width , The unit is px yo .
design sketch :

边栏推荐
- pip更新一个package
- Can the formatted data of the USB flash disk be recovered? How to recover the formatted data of the USB flash disk
- RequestContextHolder
- RequestContextHolder
- 网络参数管理
- 数的三次方根
- SQL报错盲注实例分析
- Redis中的数据类型
- Mria + RLOG 新架构下的 EMQX 5.0 如何实现 1 亿 MQTT 连接
- How to use selenium.chrome to realize the extended function of intercepting or forwarding requests
猜你喜欢

三种缓存策略:Cache Aside 策略、Read/Write Through 策略、Write Back 策略

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

C language function (1)

轻松带你走进turtle绘图的大门

押注全场景,荣耀胜算几何?

Redis事务与锁机制

获取一个控件宽度

Restclient operation index library - initialize restclient

为什么有的人把代码写的如此复杂?

Live broadcast preview | live broadcast Seminar on open source security governance models and tools
随机推荐
c语言扫雷
挖财和启牛都是干什么的开户安全吗?
阿里云国际版账户收到账号风险通知,怎么办?
postgresql数据库主从部署 主库挂了重新还原主库
flink使用ListState实现KeyedState
Comment synchroniser
Redis 配置文件
U盘被格式化数据能恢复吗,U盘被格式化了怎样恢复
LC:剑指 Offer 39. 数组中出现次数超过一半的数字
开发者分享|MindSpore Lite 体验,一键实现图像分割
Spark troubleshooting -precondition eof: no length prefix available
Strings in C language
半定制数字反相器版图绘制
Restclient operation index library - initialize restclient
Tensorrt plug-in practice (1)
【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析
怎么使用selenium.chrome实现扩展拦截或转发请求功能
笔者认为,元宇宙与互联网的发展逻辑在某种成都上是截然不同的
Google Earth engine app - a complete map legend app (land use classification of western United States)
第三章 栈