Android 如何在运行时动态设置按钮宽度以填充屏幕?

Android 如何在运行时动态设置按钮宽度以填充屏幕?,android,android-layout,Android,Android Layout,我正在学习android开发,我正在尝试用六个按钮填充屏幕。但我想动态设置按钮大小。我正在使用 final DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); final int screenWidthInDp = (int) (displayMetrics.widthPixels / displayMetrics.density); 我试图使用 button.setWidth(screenWidthInDp/

我正在学习android开发,我正在尝试用六个按钮填充屏幕。但我想动态设置按钮大小。我正在使用

final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
final int screenWidthInDp = (int) (displayMetrics.widthPixels / displayMetrics.density);
我试图使用

button.setWidth(screenWidthInDp/6);
我有六个按钮在每一行,我希望大小的变化取决于屏幕和六个按钮,以填补屏幕。我做错什么了吗

这是my activity.xml中按钮的外观

<Button
    android:id="@+id/bNum10"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/bNum2"
    android:layout_toRightOf="@+id/bNum0"
    android:text="@string/sign" />

这是我的布局

<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:background="@android:color/background_dark"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Calc" >

    <TextView
        android:id="@+id/Display"
        android:layout_width="fill_parent"
        android:layout_height="25sp"
        android:background="@android:color/background_light"
        android:gravity="right"
        android:text="@string/Welcome"
        android:textColor="@android:color/black"
        android:textSize="24sp" />

    <Button
        android:id="@+id/bNum2"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bNum1"
        android:layout_alignBottom="@+id/bNum1"
        android:layout_toRightOf="@+id/bNum1"
        android:text="@string/two" />

    <Button
        android:id="@+id/bNum3"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bNum2"
        android:layout_alignBottom="@+id/bNum2"
        android:layout_toRightOf="@+id/bNum2"
        android:text="@string/three" />

    <Button
        android:id="@+id/bNum5"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/bNum4"
        android:layout_toRightOf="@+id/bNum4"
        android:text="@string/five" />

    <Button
        android:id="@+id/bNum6"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bNum5"
        android:layout_alignBottom="@+id/bNum5"
        android:layout_toRightOf="@+id/bNum5"
        android:text="@string/six" />

    <Button
        android:id="@+id/bNum0"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/Display"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="14dp"
        android:text="@string/zero" />

    <Button
        android:id="@+id/bNum1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bNum0"
        android:layout_alignLeft="@+id/bNum0"
        android:text="@string/one" />

    <Button
        android:id="@+id/bNum4"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bNum1"
        android:layout_alignLeft="@+id/bNum1"
        android:text="@string/four" />

    <Button
        android:id="@+id/bNum7"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bNum5"
        android:layout_alignParentLeft="true"
        android:text="@string/seven" />

    <Button
        android:id="@+id/bNum8"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bNum5"
        android:layout_toLeftOf="@+id/bNum6"
        android:text="@string/eight" />

    <Button
        android:id="@+id/bNum9"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/bNum8"
        android:layout_alignLeft="@+id/bNum6"
        android:text="@string/nine" />

    <Button
        android:id="@+id/bNum10"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bNum2"
        android:layout_toRightOf="@+id/bNum0"
        android:text="@string/sign" />

    <Button
        android:id="@+id/bEqual"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bExp"
        android:layout_alignBottom="@+id/bExp"
        android:layout_alignParentRight="true"
        android:text="@string/equal" />

    <Button
        android:id="@+id/bDot"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bNum10"
        android:layout_alignBottom="@+id/bNum10"
        android:layout_toRightOf="@+id/bNum10"
        android:text="@string/dot" />

    <Button
        android:id="@+id/bExp"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bDot"
        android:layout_alignBottom="@+id/bDot"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/bDot"
        android:text="@string/exp" />

</RelativeLayout>


如何解决此问题?

使用linearlayout和weight显示6个宽度相等的按钮

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal" android:weightSum="6" >

     <Button

        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_weight="1" />

     <Button
                   android:layout_width="0dp"
        android:layout_height="wrap_content"
                  android:layout_weight="1" />
  <Button
                       android:layout_width="0dp"
            android:layout_height="wrap_content"
                      android:layout_weight="1" />
  <Button
                       android:layout_width="0dp"
            android:layout_height="wrap_content"
                      android:layout_weight="1" />
  <Button
                       android:layout_width="0dp"
            android:layout_height="wrap_content"
                      android:layout_weight="1" />
  <Button
                       android:layout_width="0dp"
            android:layout_height="wrap_content"
                      android:layout_weight="1" />


  </LinearLayout>

使用线性布局和权重显示6个宽度相等的按钮

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal" android:weightSum="6" >

     <Button

        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_weight="1" />

     <Button
                   android:layout_width="0dp"
        android:layout_height="wrap_content"
                  android:layout_weight="1" />
  <Button
                       android:layout_width="0dp"
            android:layout_height="wrap_content"
                      android:layout_weight="1" />
  <Button
                       android:layout_width="0dp"
            android:layout_height="wrap_content"
                      android:layout_weight="1" />
  <Button
                       android:layout_width="0dp"
            android:layout_height="wrap_content"
                      android:layout_weight="1" />
  <Button
                       android:layout_width="0dp"
            android:layout_height="wrap_content"
                      android:layout_weight="1" />


  </LinearLayout>

@vipul mittal有答案,如果您不需要以编程方式进行设置。然而,由于您也问“我做错了什么”,所以您没有使用dp设置按钮宽度-它是像素

因此,当您获得显示指标时,只需将像素除以6,如下所示:

button.setWidth((int) (displayMetrics.widthPixels/6));

@如果您不需要通过编程方式进行设置,vipul mittal可以提供答案。然而,由于您也问“我做错了什么”,所以您没有使用dp设置按钮宽度-它是像素

因此,当您获得显示指标时,只需将像素除以6,如下所示:

button.setWidth((int) (displayMetrics.widthPixels/6));

使用线性布局将是XML中的最佳选择。像这样:

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal" 
     android:weightSum="1" >

     <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.15" />

     <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.15" />

       // Other buttons and texview in similar way. Total sum of android:layout_weight values need to be 1.
  </LinearLayout>

//其他按钮和文本视图以类似的方式显示。android:layout_权重值的总和必须为1。
似乎您更感兴趣的是在TableLayout中,每行有6个按钮和1个文本视图。我希望您以这种方式更改xml中的内容:

<TableLayout
            android:id="@+id/tableLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:shrinkColumns="*"
            android:stretchColumns="*" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <Button
                    android:id="@+id/button1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_span="3" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_span="3" />

                    // Other buttons and texview in similar way

            </TableRow>      

//其他按钮和文本视图以类似方式显示

使用线性布局将是XML中的最佳选择。像这样:

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal" 
     android:weightSum="1" >

     <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.15" />

     <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.15" />

       // Other buttons and texview in similar way. Total sum of android:layout_weight values need to be 1.
  </LinearLayout>

//其他按钮和文本视图以类似的方式显示。android:layout_权重值的总和必须为1。
似乎您更感兴趣的是在TableLayout中,每行有6个按钮和1个文本视图。我希望您以这种方式更改xml中的内容:

<TableLayout
            android:id="@+id/tableLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:shrinkColumns="*"
            android:stretchColumns="*" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <Button
                    android:id="@+id/button1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_span="3" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_span="3" />

                    // Other buttons and texview in similar way

            </TableRow>      

//其他按钮和文本视图以类似方式显示


请您详细说明我是如何做到这一点的?我每行有6个按钮,不是全部。这是我的错。我想填充每一行,将所有按钮的宽度设置为0dp,重量设置为1。还将linearlayout的权重和设置为6。让您想要的其他属性(id、文本等)等待。我也有一个文本视图内的布局,我不想被改变。还有,你说的休息是什么意思?(我的英语不太好)也发布你的布局。你的布局中有6个以上的按钮。你能画出什么样的布局???您希望哪6个按钮排成一行?请您详细说明我是如何做到这一点的?我每行有6个按钮,不是全部。这是我的错。我想填充每一行,将所有按钮的宽度设置为0dp,重量设置为1。还将linearlayout的权重和设置为6。让您想要的其他属性(id、文本等)等待。我也有一个文本视图内的布局,我不想被改变。还有,你说的休息是什么意思?(我的英语不太好)也发布你的布局。你的布局中有6个以上的按钮。你能画出什么样的布局???你想把哪个6键排成一行?我们不是应该用dp吗?这就是我在android教程中读到的。是的,我是XML中的noob思想。但是,从编程的角度来看,按钮小部件是以像素为单位完成的:试过之后,没有什么区别。我没有检查你的布局,但仔细看,你的父布局中有空白,按钮没有真正对齐。你需要减去页边空白,并可能在页边空白处加上空格。我的意思是说displayMetrics是整个屏幕的宽度,但是您的按钮的父布局占用了一些空间-它们不会正确排列,我们不应该使用dp吗?这就是我在android教程中读到的。是的,我是XML中的noob思想。但是,从编程的角度来看,按钮小部件是以像素为单位完成的:试过之后,没有什么区别。我没有检查你的布局,但仔细看,你的父布局中有空白,按钮没有真正对齐。你需要减去页边空白,并可能在页边空白处加上空格。我的意思是说displayMetrics是整个屏幕的宽度,但是您的按钮的父布局占用了一些空间-它们不会正确排列只有一个textview,每行六个按钮,现在没有当前的按钮数,我会添加更多,所以你不知道每行有多少个按钮?每行有六个按钮,我不知道行数。在这种情况下,你应该尝试使用ListView并在ListView中添加一行的布局,或者查看以下帖子:只有一个textview,每行有六个按钮,现在没有按钮的当前数量,我将添加更多按钮,因此您不知道每行有多少个按钮?每行有六个按钮,我不知道行数。在这种情况下,您应该尝试使用ListView并在ListView中添加一行的布局,或者查看以下帖子: