设置按钮大小
Setting button size
我是 android 的新人。我遇到了一个问题,即如何将按钮设置为彼此完全相邻并自动调用,因此每台设备的两侧或按钮之间没有空 space。
我试图通过这种方式解决该问题:找到 returns 屏幕(或父级?)宽度和高度的方法,然后创建 4 个宽度为 1/4 屏幕的容器,并创建按钮 'fill parent'。如果无法创建容器,我可以创建宽度为 1/4 屏幕的按钮。
有returns屏幕或应用大小的方法吗?是否可以在 Java 代码中设置按钮的大小?
您可以使用此代码获取屏幕的像素尺寸:
Display display = getWindowManager().getDefaultDisplay();
Point screen = new Point();
display.getSize(screen);
int width = screen.x;
int height = screen.y;
除此之外,对于您的问题而不是此代码,您可以将 Buttons 宽度设置为 0 并使用 Button.weight属性它会自动在您的按钮之间共享 space。
您可以像这样简单地使用 LinearLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="+@id/btn1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="BUTTON 1"/>
<Button
android:id="+@id/btn2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="BUTTON 2"/>
<Button
android:id="+@id/btn3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="BUTTON 3"/>
<Button
android:id="+@id/btn4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="BUTTON 4"/>
</LinearLayout>
通过这种设计,您的 Button 的高度将匹配整个屏幕。您可以更改其父 LinearLayout 的高度并在其下方或上方放置一些其他元素
注意:根据您为应用程序使用的主题,按钮的背景可能会发生变化。因此,如果您需要没有任何边距的平坦背景,则需要使用 theme.holo.light 或创建自己的背景并像这样为 Button 设置它并将其设置为 Buttton 的背景:
background.xml
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" >
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#ffFDE234"
android:endColor="#ffFDE234"
android:angle="270"/>
</shape>
</item>
<item>
<shape >
<gradient
android:startColor="#00000000"<!--transparent color as default color for Button's Background, you can change it-->
android:endColor="#00000000"
android:angle="270"/>
</shape>
</item>
</selector>
对于线性布局,请在您的 xml:
中使用此代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
将每个按钮的权重设置为 1 将意味着它们都占用相等的空间 space
按钮之间没有空 space:
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:text="Button1"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:text="Button2"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:text="Button3"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:text="Button4"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_weight="1"/>
我是 android 的新人。我遇到了一个问题,即如何将按钮设置为彼此完全相邻并自动调用,因此每台设备的两侧或按钮之间没有空 space。
我试图通过这种方式解决该问题:找到 returns 屏幕(或父级?)宽度和高度的方法,然后创建 4 个宽度为 1/4 屏幕的容器,并创建按钮 'fill parent'。如果无法创建容器,我可以创建宽度为 1/4 屏幕的按钮。
有returns屏幕或应用大小的方法吗?是否可以在 Java 代码中设置按钮的大小?
您可以使用此代码获取屏幕的像素尺寸:
Display display = getWindowManager().getDefaultDisplay();
Point screen = new Point();
display.getSize(screen);
int width = screen.x;
int height = screen.y;
除此之外,对于您的问题而不是此代码,您可以将 Buttons 宽度设置为 0 并使用 Button.weight属性它会自动在您的按钮之间共享 space。
您可以像这样简单地使用 LinearLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="+@id/btn1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="BUTTON 1"/>
<Button
android:id="+@id/btn2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="BUTTON 2"/>
<Button
android:id="+@id/btn3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="BUTTON 3"/>
<Button
android:id="+@id/btn4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="BUTTON 4"/>
</LinearLayout>
通过这种设计,您的 Button 的高度将匹配整个屏幕。您可以更改其父 LinearLayout 的高度并在其下方或上方放置一些其他元素
注意:根据您为应用程序使用的主题,按钮的背景可能会发生变化。因此,如果您需要没有任何边距的平坦背景,则需要使用 theme.holo.light 或创建自己的背景并像这样为 Button 设置它并将其设置为 Buttton 的背景:
background.xml
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" >
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#ffFDE234"
android:endColor="#ffFDE234"
android:angle="270"/>
</shape>
</item>
<item>
<shape >
<gradient
android:startColor="#00000000"<!--transparent color as default color for Button's Background, you can change it-->
android:endColor="#00000000"
android:angle="270"/>
</shape>
</item>
</selector>
对于线性布局,请在您的 xml:
中使用此代码<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
将每个按钮的权重设置为 1 将意味着它们都占用相等的空间 space
按钮之间没有空 space:
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:text="Button1"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:text="Button2"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:text="Button3"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="50dp"
android:text="Button4"
android:layout_marginLeft="-5dip"
android:layout_marginRight="-5dip"
android:layout_weight="1"/>