Android开发_精准排布控件位置

1.      简述

在Android系统上开发程序,很多时候需要精准的排布控件的位置和大小.并且适合各种比例的屏幕(4:3,16:9…),下面分别介绍在高版本和低版本的Android中的实现方法.

2.      Android Studio/高版本Android实现

1)             说明
使用高版本android内置的android-support-percent-lib库,通过设置百分比的方法,实现了该功能, Demo一般都是android studio,Eclipse下需要下载支持库:http://download.csdn.net/detail/sbsujjbcy/8857747
在Layout中设置百分比: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>
3)             相关属性:

layout_widthPercent

layout_heightPercent

layout_marginPercent

layout_marginLeftPercent

layout_marginTopPercent

layout_marginRightPercent

layout_marginBottomPercent

layout_marginStartPercent

layout_marginEndPercent

4)             修改build.gradle
在dependencies{}加
compile 'com.android.support:percent:22.2.0'

3.      使用Eclipse/低版本Android实现

1)             说明
推荐用此方法,它的通用性更强,适合各个Android版本,无需外加扩展库,对于layout按一定比例或规则排列的界面更方便使用。

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>

3)             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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值