[英]Android make button 1/3 of screen width and height in relative layout
我有一些XML。 在此XML中,相對布局內只有一個按鈕。 相對布局占據了整個屏幕。 我希望按鈕具有屏幕寬度和高度的1/3。 我該怎么辦?
這是XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
tools:context="com.vroy.trapper.MainMenuActivity"
android:layout_weight="2">
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="@string/menu_button_text"
android:background="@drawable/round_button"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
我看着這個問題,但是即使我為相對布局本身分配了權重,也無法為按鈕分配權重。
您可以在Java代碼中輕松完成此操作。
將此代碼編寫在oncreate中。
Button b=(Button) findViewById(R.id.button);
int width = getResources().getDisplayMetrics().widthPixels/3;
int hei=getResources().getDisplayMetrics().heightPixels/3;
b.setLayoutParams(new RelativeLayout.LayoutParams(width,hei));
所需的百分比支持庫。
該庫非常易於使用,因為它與我們熟悉的RelativeLayout和FrameLayout相同,只是具有一些其他功能。
首先,由於百分比支持庫與Android支持庫23一起提供,因此請確保已將SDK Manager中的Android支持庫更新為最新版本。 然后在build.gradle文件中添加如下所示的依賴項 :
編譯'com.android.support:percent:23.0.0'
現在回到您的問題,您可以執行以下操作以實現輸出。 希望對您有幫助。
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout 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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/bird"
android:text="Your text"
app:layout_heightPercent="33%"
app:layout_widthPercent="33%" />
</android.support.percent.PercentRelativeLayout>
(編輯時也考慮寬度)將按鈕環繞在線性布局上。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:weightSum="3" >
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:weightSum="3">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_weight="1"
android:text="@string/menu_button_text"
android:background="@drawable/round_button"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
如果您希望按鈕位於左右等位置,請更改android:gravity
。使用此方法,您仍然可以將相對布局用於其周圍的其他內容。
使用LinearLayout而不是RelativeLayout可以實現您的目標。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/menu_button_text"
android:background="@drawable/round_button"
android:id="@+id/button"/>
<View
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
希望能幫助到你!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.