设置视图的宽高

AndroidManifest.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity
            android:name=".ViewBorderActivity"
            android:exported="false" />
        <activity
            android:name=".TextSizeActivity"
            android:exported="true" />
    </application>

</manifest>

activity_view_border.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#00ffff"
        android:text="视图宽高采用wrap_content定义"
        android:textColor="#000000"
        android:textSize="17sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#00ffff"
        android:text="视图宽高采用match_parent定义"
        android:textColor="#000000"
        android:textSize="17sp" />

    <TextView
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#00ffff"
        android:text="视图宽度采用固定大小"
        android:textColor="#000000"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/tv_code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#00ffff"
        android:text="通过代码指定视图宽度"
        android:textColor="#000000"
        android:textSize="17sp" />

</LinearLayout>

ViewBorderActivity.java

复制代码
package com.example.chapter03;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.chapter03.util.Utils;

public class ViewBorderActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_border);
        TextView tv_code = findViewById(R.id.tv_code);
        //获取tv_code的布局参数(含宽度和高度)
        ViewGroup.LayoutParams params = tv_code.getLayoutParams();
        //修改布局参数中的宽度数值,注意默认px单位,需要吧dp数值转成px数值
        params.width = Utils.dip2px(this,300);
        //设置tv_code的布局参数
        tv_code.setLayoutParams(params);

    }
}

Utils.java

复制代码
package com.example.chapter03.util;

import android.content.Context;

public class Utils {

    public static int dip2px(Context context, float dpValue){
     //获取当前手机的像素密度(1个dp对应几个px)
      float scale =context.getResources().getDisplayMetrics().density;
       return  (int)(dpValue*scale + 0.5f);
    }
}
相关推荐
尚墨1111几秒前
Java RestTemplate报错Invalid mime type “charset=utf-8“: does not contain ‘/‘
java·开发语言
我命由我123451 分钟前
Java 开发使用 MyBatis PostgreSQL 问题:传入的参数为 null,CONCAT 函数无法推断参数的数据类型
java·开发语言·数据库·学习·postgresql·mybatis·学习方法
爱装代码的小瓶子2 分钟前
【c++知识铺子】map和set的底层-红黑树
java·开发语言·c++
小蒜学长2 分钟前
基于Spring Boot家政服务系统的设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端
洛阳泰山3 分钟前
Java实现周易六爻自动排盘:根据卜卦的时间推算出天干地支
java·开发语言·周易·六爻
apihz5 分钟前
随机英文姓名生成API接口详细教程:免费、简单、高效
android·java·运维·服务器·开发语言
a程序小傲8 分钟前
百度Java面试被问:HTTPS解决了HTTP什么问题?
java·后端·http·百度·面试
曹牧10 分钟前
Java:类的前20个字段转换为Json
java·开发语言·python
netyeaxi10 分钟前
SpringBoot:SpringBoot2.7.x如何将logback升级到1.3.x以上版本
java·spring boot·logback
小王师傅6612 分钟前
【轻松入门SpringBoot】从0到1搭建web 工程(下)-在实践中对比SpringBoot和Spring框架
java·spring boot·spring