【问题标题】:How can i dynamically size buttons according to the screen size?如何根据屏幕大小动态调整按钮大小?
【发布时间】:2017-05-15 17:15:20
【问题描述】:

我在表格布局中有按钮。我在这里将按钮动态地放在表格布局中

 TableLayout tblHotels=(TableLayout) findViewById(R.id.tblLayout);
        if(hotels != null){
            int index=0;
            for (int i=0; i<(hotels.length/2); i++) {
                TableRow tblRow=new TableRow(getApplicationContext());
                tblHotels.addView(tblRow);
                tblRow.setLayoutParams(new TableLayout.LayoutParams(
                        TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT));
                for (int j=0;j<2; j++)
                {
                    tblRow.addView(hotels[index]);
                    hotels[index].setLayoutParams(new TableRow.LayoutParams(
                            TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT, 1.0F));
                    hotels[index].setOnClickListener(this);
                    index++;
                }
            }
        }

但它显示在屏幕上,我希望我的按钮具有相同的宽度和更大的高度。我该怎么做?我的xmlfile现在是这样的。

`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.arzucaki.sherwoodhotels.Hotels"
    android:weightSum="1"
    >


    <TableLayout
        android:id="@+id/tblLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.6"
        android:gravity="bottom"
        android:orientation="vertical"
        android:layout_alignParentTop="true">


    </TableLayout>




</RelativeLayout>`

【问题讨论】:

  • 如果有 100 家(意味着更多)酒店,则表格布局将无法正确显示。最好使用list/recyclerview/gridview布局。

标签: android button size tablelayout dynamically-generated


【解决方案1】:

您可以继续在 xml 文件中创建表格行,并在 dimens.xml 文件中定义表格行的高度:

这是一个示例表示:

 <TableRow
        android:layout_width="match_parent"
        android:layout_height="@dimen/table_row_height">

        <TextView
            android:text="Time"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_column="1" />

        <TextView
            android:text="Time"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_column="2" />
    </TableRow>

为您希望在其各自文件夹中支持的所有分辨率添加dimens.xml 文件。 例如。值,值-w820dp 等。 只需在这些文件夹中创建一个名为 dimens 的新文件,然后像这样声明您的值:

<resources>
<!-- Table Row height -->
<dimen name="table_row_height">16dp</dimen>

根据您的要求,该值在 values-w820dp 下的另一个文件中会更高,可能是这样的:

    <resources>
<!-- Table Row height -->
<dimen name="table_row_height">24dp</dimen>

【讨论】:

  • 所有按钮都只是用重量 1 填充屏幕。我只是想知道我怎样才能给出一个特殊的高度,比如屏幕的百分比,在所有具有不同屏幕尺寸的设备上运行相同。
  • 在这种情况下,您需要在 xml 文件中定义这些表格行,并在 dimens 文件中提供与 mdpi、hdpi、xhdpi、xxhdpi 和 xxxhdpi 等设备的不同分辨率相对应的尺寸尺寸,您也可以继续将 match_parent 更改为 wrap_content 并提供一些填充,这样控件就不会占据整个屏幕并提供一些填充。但是对于您的尺寸应该根据设备分辨率而变化的要求,您必须使用尺寸文件。
  • hımmm 这很有道理,所以你能在你的答案中举一个例子,这样我就可以尝试将你的答案标记为正确答案。
猜你喜欢
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 2017-01-19
  • 1970-01-01
  • 2021-11-21
相关资源
最近更新 更多