Android 在相对布局中使按钮成为屏幕宽度和高度的 1/3
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>
我查看了 this 问题,但即使我为相对布局本身分配了权重,我也无法为按钮分配权重。
使用 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>
希望对您有所帮助!!
(编辑以考虑宽度)
将按钮包裹在线性布局周围。
<?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>
如果您希望您的按钮位于 right/left 等位置,请更改 android:gravity
。有了这个,您仍然可以将相对布局用于它周围的其他内容。
您可以在 java 代码中轻松做到这一点。
在oncreate中写这段代码。
Button btn = (Button) findViewById(R.id.button);
int width = getResources().getDisplayMetrics().widthPixels/3;
int height = getResources().getDisplayMetrics().heightPixels/3;
btn.setLayoutParams(new RelativeLayout.LayoutParams(width, heigth));
Percent 支持库正是您所需要的。
这个库非常容易使用,因为它与我们熟悉的 RelativeLayout 和 FrameLayout 相同,只是增加了一些额外的功能。
首先,由于 Percent Support Library 随 Android Support Library 23 一起提供,因此请确保您已将 SDK Manager 中的 Android Support Library 更新至最新版本。然后在 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。在这个 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>
我查看了 this 问题,但即使我为相对布局本身分配了权重,我也无法为按钮分配权重。
使用 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>
希望对您有所帮助!!
(编辑以考虑宽度) 将按钮包裹在线性布局周围。
<?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>
如果您希望您的按钮位于 right/left 等位置,请更改 android:gravity
。有了这个,您仍然可以将相对布局用于它周围的其他内容。
您可以在 java 代码中轻松做到这一点。
在oncreate中写这段代码。
Button btn = (Button) findViewById(R.id.button);
int width = getResources().getDisplayMetrics().widthPixels/3;
int height = getResources().getDisplayMetrics().heightPixels/3;
btn.setLayoutParams(new RelativeLayout.LayoutParams(width, heigth));
Percent 支持库正是您所需要的。
这个库非常容易使用,因为它与我们熟悉的 RelativeLayout 和 FrameLayout 相同,只是增加了一些额外的功能。
首先,由于 Percent Support Library 随 Android Support Library 23 一起提供,因此请确保您已将 SDK Manager 中的 Android Support Library 更新至最新版本。然后在 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>