치수(dimen) 리소스 사용
치수 단위는 다음과 같습니다.
1. 픽셀(px): 화면의 실제 픽셀 표시
2.피트(in): 화면 기반 실제 크기
3.cm(mm): 화면 기반 실제 크기
4. 점(pt): 피트의 1/72
5.dp: 밀도와 무관한 픽셀, 화면의 물리적 밀도에 대한 추상적인 단위
6.sp: 정밀도와 무관한 픽셀, dp와 유사
다음은 하나의 실례를 통해 사이즈 자원의 사용법을 보여 준다.레이아웃 파일에 TextView와 Button을 추가합니다.TextView의 너비 및 높은 참조 치수 리소스를 설정합니다.Button의 폭과 높이는 코드에 설정됩니다.
코드는 다음과 같습니다.
package com.lovo;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.Button;
public class TestDimensActivity extends Activity {
	private Button myButton;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//     Activity       
		setContentView(R.layout.main);
		//   findViewById    Button  
		myButton = (Button) findViewById(R.id.btn);
		//   Resources  
		Resources r = getResources();
		//   getDimension      
		float btn_h = r.getDimension(R.dimen.btn_height);
		float btn_w = r.getDimension(R.dimen.btn_width);
		//       
		myButton.setHeight((int) btn_h);
		//       
		myButton.setWidth((int) btn_w);
	}
}<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:height="@dimen/text_height"
        android:text="       "
        android:width="@dimen/text_width" />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="       " />
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="text_width">150px</dimen>
    <dimen name="text_height">100px</dimen>
    <dimen name="btn_width">30mm</dimen>
    <dimen name="btn_height">10mm</dimen>
</resources>이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.