日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 界面布局之RelativeLayout

發布時間:2025/3/20 Android 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 界面布局之RelativeLayout 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


Android 的 RelaliveLayout 布局的參數定義:


android:layout_above="@id/xxx"? --將控件置于給定ID控件之上
android:layout_below="@id/xxx"? --將控件置于給定ID控件之下

android:layout_toLeftOf="@id/xxx"? --將控件的右邊緣和給定ID控件的左邊緣對齊
android:layout_toRightOf="@id/xxx"? --將控件的左邊緣和給定ID控件的右邊緣對齊

android:layout_alignLeft="@id/xxx"? --將控件的左邊緣和給定ID控件的左邊緣對齊
android:layout_alignTop="@id/xxx"? --將控件的上邊緣和給定ID控件的上邊緣對齊
android:layout_alignRight="@id/xxx"? --將控件的右邊緣和給定ID控件的右邊緣對齊
android:layout_alignBottom="@id/xxx"? --將控件的底邊緣和給定ID控件的底邊緣對齊
android:layout_alignParentLeft="true"? --將控件的左邊緣和父控件的左邊緣對齊
android:layout_alignParentTop="true"? --將控件的上邊緣和父控件的上邊緣對齊
android:layout_alignParentRight="true"? --將控件的右邊緣和父控件的右邊緣對齊
android:layout_alignParentBottom="true" --將控件的底邊緣和父控件的底邊緣對齊
android:layout_centerInParent="true"? --將控件置于父控件的中心位置
android:layout_centerHorizontal="true"? --將控件置于水平方向的中心位置
android:layout_centerVertical="true"? --將控件置于垂直方向的中心位置



編程實例:



定義布局 activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:id="@+id/r1"

? ? android:layout_width="fill_parent"

? ? android:layout_height="fill_parent"

? ? android:background="#edab4a" >


? ? <Button

? ? ? ? android:id="@+id/Shang"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_alignParentTop="true"

? ? ? ? android:layout_centerHorizontal="true"

? ? ? ? android:text="@string/shang" />


? ? <Button

? ? ? ? android:id="@+id/Xia"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_alignParentBottom="true"

? ? ? ? android:layout_centerHorizontal="true"

? ? ? ? android:text="@string/xia" />


? ? <Button

? ? ? ? android:id="@+id/Zuo"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_alignParentLeft="true"

? ? ? ? android:layout_centerVertical="true"

? ? ? ? android:text="@string/zuo" />


? ? <Button

? ? ? ? android:id="@+id/You"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_alignParentRight="true"

? ? ? ? android:layout_centerVertical="true"

? ? ? ? android:text="@string/you" />


? ? <ImageButton

? ? ? ? android:contentDescription="@string/wutu"

? ? ? ? android:id="@+id/Start"

? ? ? ? android:layout_width="60dip"

? ? ? ? android:layout_height="100dip"

? ? ? ? android:layout_centerInParent="true"

? ? ? ? android:src="@drawable/fengjing" />


</RelativeLayout>


字符串資源 sring.xml


<?xml version="1.0" encoding="utf-8"?>

<resources>


? ? <string name="app_name">RelativeLayout</string>

? ? <string name="action_settings">Settings</string>

? ? <string name="hello_world">Hello world!</string>

? ? <string name="shang">上</string>

? ? <string name="xia">下</string>

? ? <string name="zuo">左</string>

? ? <string name="you">右</string>

? ? <string name="wutu">無圖</string>

? ? <string name="shuoming">圖片說明</string>


</resources>


主活動 ?MainActivity.java


package com.malakana.relativelayout;


import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.RelativeLayout;

import android.app.Activity;


public class MainActivity extends Activity {


RelativeLayout r1;

Button shang;

Button xia;

Button zuo;

Button you;

ImageView currButton;

ImageView start;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


r1 = (RelativeLayout) findViewById(R.id.r1);

shang = (Button) findViewById(R.id.Shang);

xia = (Button) findViewById(R.id.Xia);

zuo = (Button) findViewById(R.id.Zuo);

you = (Button) findViewById(R.id.You);

start = (ImageView) findViewById(R.id.Start);

currButton = start;


shang.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

EditText temp = new EditText(MainActivity.this);

temp.setText(R.string.shuoming);

//設置控件位置

RelativeLayout.LayoutParams lp_1 =?

new RelativeLayout.LayoutParams(

ViewGroup.LayoutParams.WRAP_CONTENT,95);

lp_1.addRule(RelativeLayout.ABOVE,currButton.getId()); //ABOVE ?在currButton之上

lp_1.addRule(RelativeLayout.CENTER_HORIZONTAL,currButton.getId());

//將控件添加到布局中

r1.addView(temp,lp_1);

}});

xia.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

EditText temp = new EditText(MainActivity.this);

temp.setText(R.string.shuoming);

//設置控件位置

RelativeLayout.LayoutParams lp_1 =?

new RelativeLayout.LayoutParams(

ViewGroup.LayoutParams.WRAP_CONTENT,95);

lp_1.addRule(RelativeLayout.BELOW,currButton.getId()); //BELOW ?在currButton之下

lp_1.addRule(RelativeLayout.CENTER_HORIZONTAL,currButton.getId());

//將控件添加到布局中

r1.addView(temp,lp_1);

}});

zuo.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

EditText temp = new EditText(MainActivity.this);

temp.setText(R.string.shuoming);

//設置控件位置

RelativeLayout.LayoutParams lp_1 =?

new RelativeLayout.LayoutParams(

ViewGroup.LayoutParams.WRAP_CONTENT,95);

lp_1.addRule(RelativeLayout.LEFT_OF,currButton.getId()); //LEFT_OF ?將控件的右邊緣和currButton的左邊緣對齊

lp_1.addRule(RelativeLayout.CENTER_VERTICAL,currButton.getId()); ?//將控件置于垂直方向的中心位置

//將控件添加到布局中

r1.addView(temp,lp_1);

}});

you.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

EditText temp = new EditText(MainActivity.this);

temp.setText(R.string.shuoming);

//設置控件位置

RelativeLayout.LayoutParams lp_1 =?

new RelativeLayout.LayoutParams(

ViewGroup.LayoutParams.WRAP_CONTENT,95);

lp_1.addRule(RelativeLayout.RIGHT_OF,currButton.getId()); //RIGHT_OF ?將控件的左邊緣和currButton的右邊緣對齊

lp_1.addRule(RelativeLayout.CENTER_VERTICAL,currButton.getId()); ?//將控件置于垂直方向的中心位置

//將控件添加到布局中

r1.addView(temp,lp_1);

}});

}


}



效果圖:

轉載于:https://blog.51cto.com/taiyi928/1550159

總結

以上是生活随笔為你收集整理的Android 界面布局之RelativeLayout的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。