Android開發(fā)_精準(zhǔn)排布控件位置

1. 簡述

在Android系統(tǒng)上開發(fā)程序,很多時(shí)候需要精準(zhǔn)的排布控件的位置和大?。⑶疫m合各種比例的屏幕(4:3,16:9…),下面分別介紹在高版本和低版本的Android中的實(shí)現(xiàn)方法.

2. Android Studio/高版本Android實(shí)現(xiàn)

  1. 說明
    使用高版本android內(nèi)置的android-support-percent-lib庫,通過設(shè)置百分比的方法,實(shí)現(xiàn)了該功能, Demo一般都是android studio,Eclipse下需要下載支持庫:http://download.csdn.net/detail/sbsujjbcy/8857747
    在Layout中設(shè)置百分比:PercentRelativeLayout/PercentFrameLayout/PercentLinearLayout
  2. Layout文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#44ff0000"
        android:text="width:30%,height:20%"
        app:layout_heightPercent="20%"
        app:layout_marginLeftPercent="10%"
        app:layout_marginTopPercent="40%"
        app:layout_widthPercent="30%"/>
</android.support.percent.PercentFrameLayout>
  1. 相關(guān)屬性:
    layout_widthPercent
    layout_heightPercent
    layout_marginPercent
    layout_marginLeftPercent
    layout_marginTopPercent
    layout_marginRightPercent
    layout_marginBottomPercent
    layout_marginStartPercent
    layout_marginEndPercent
  2. 修改build.gradle
    在dependencies{}加
    compile 'com.android.support:percent:22.2.0'

3. 使用Eclipse/低版本Android實(shí)現(xiàn)

  1. 說明
    推薦用此方法,它的通用性更強(qiáng),適合各個(gè)Android版本,無需外加擴(kuò)展庫,對于layout按一定比例或規(guī)則排列的界面更方便使用。
  2. Layout文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/txt"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="left|top"
        android:background="#44ff0000"
        android:text="testme"/>

</RelativeLayout>
  1. Java程序
package com.test.testme;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.RelativeLayout;

public class testme extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView txtView = (TextView) findViewById(R.id.txt);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)txtView.getLayoutParams(); 
        params.width = 100;
        params.height = 100;
        params.setMargins(100,200,0,0);
        txtView.setLayoutParams(params);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容