设置按钮大小

时间:2015-02-14 19:54:00

标签: android android-layout

我是android的新手。我遇到了一个问题,即如何将按钮完全设置在彼此旁边,自动进行调整,因此每个设备的侧面或按钮之间没有空白区域。

我试图通过这种方式解决这个问题:查找返回屏幕(或父级?)宽度和高度的方法,然后创建宽度为1/4屏幕的4个容器,并创建“填充父级&#的按钮” 39 ;.如果创建容器是不可能的,我可以创建宽度为1/4屏幕的按钮。

是否有返回屏幕或应用尺寸的方法?是否可以在Java代码中设置按钮的大小?

4 个答案:

答案 0 :(得分:0)

您可以使用此代码以像素为单位获取屏幕尺寸:

Display display = getWindowManager().getDefaultDisplay();
Point screen = new Point();
display.getSize(screen);
int width = screen.x;
int height = screen.y;

除此之外,对于您的问题而不是此代码,您可以将按钮宽度设置为0并使用 Button.weight 属性,它会自动在按钮之间共享空间。< / p>

答案 1 :(得分:0)

您可以像这样简单地使用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的高度,并把一些其他元素放在它下面或上面

注意:根据您用于应用的主题,可能会更改Button的背景。因此,如果您需要没有任何边距的平坦背景,您需要使用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>

答案 2 :(得分:0)

使用linearlayout,在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意味着它们都占据相等的空间

答案 3 :(得分:0)

按钮之间没有空格:

enter image description here

<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"/>