如何根据屏幕尺寸动态调整按钮大小?
How can i dynamically size buttons according to the screen size?
我在 table 布局中有按钮。我将按钮动态地放在 table 布局中
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>`
您可以继续在 xml 文件中创建 table 行,并在 dimens.xml 文件中定义 table 行的高度:
这是一个示例表示:
<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>
我在 table 布局中有按钮。我将按钮动态地放在 table 布局中
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>`
您可以继续在 xml 文件中创建 table 行,并在 dimens.xml 文件中定义 table 行的高度:
这是一个示例表示:
<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>