android中按钮的响应式定位

时间:2013-06-11 21:19:36

标签: android android-layout android-linearlayout

我正在使用Dialog向用户显示一些选项。我已经夸大了xml内容的一些按钮。在横向模式下,按钮显示如下:land_dialog

在纵向模式下,

按钮显示在一行中。但由于屏幕无法容纳所有按钮,因此某些按钮未显示。我希望按钮处于纵向模式,如下所示:portrait_dialog

这意味着响应。 但我该怎么做?我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/jinish" >

     <Button
            android:id="@+id/sound2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:layout_marginLeft="2dp"
            android:background="@drawable/sound_xml"/>

            <Button
            android:id="@+id/review2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:background="@drawable/review_xml"/>

            <Button
            android:id="@+id/instraction"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="2dp"
            android:background="@drawable/instraction_xml"/>            

            <Button
            android:id="@+id/solve"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/solve_xml"
            android:layout_marginRight="2dp"/>  

            <Button
            android:id="@+id/reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/reset_xml"
            android:layout_marginRight="2dp"/>

</LinearLayout>

和我的java代码:

private LinearLayout options_layout;
private Dialog options_show;

options_layout = (LinearLayout) getLayoutInflater().inflate(
                R.layout.puzzle_options, null);

options_show = new Dialog(this);
options_show.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
options_show.setContentView(options_layout);
options_show.setCancelable(true);
options_show.setCanceledOnTouchOutside(true);
options_show.getWindow().setBackgroundDrawable(new ColorDrawable(0));

options_show.show();

1 个答案:

答案 0 :(得分:0)

  • 使用GridView,并将其列设置为维度:android:numColumns="@integer/num_columns"
  • 添加到维度声明(例如values / dimens.xml):<integer name="num_columns">3</integer>
  • 添加另一个维度文件,其方向设置为landscape(例如values-land / dimens.xml):<integer name="num_columns">6</integer>

现在,根据设备方向,Android会为列数选择值3或6。

您需要创建一个基本适配器来将数据绑定到GridView

相关问题