简体   繁体   English

Android-使用按钮填充布局

[英]Android - Fill Layout with Buttons

Just a quick question about how you would go about implementing this. 只是一个关于如何实现此目标的简短问题。 I want there to be buttons at the bottom of the screen, but if the screen size is larger, more buttons would be added. 我希望屏幕底部有按钮,但是如果屏幕较大,则会添加更多按钮。

For example, at a small screen size, there might be 4-5 buttons at the bottom, but if you ran it on a tablet or something similar, there would be maybe 20 buttons. 例如,在较小的屏幕上,底部可能会有4-5个按钮,但是如果您在平板电脑或类似设备上运行它,则可能会有20个按钮。

Any suggestions? 有什么建议么? It can't scroll either, it just has to dynamically fill the layout with buttons. 它也不能滚动,只需要用按钮动态填充布局即可。

Thanks. 谢谢。

To put buttons at the bottom of a layout, do something like this to your layout: 要将按钮放在布局的底部,请对布局执行以下操作:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout">
    <LinearLayout
        android:layout_height="wrap_content"
        android:id="@+id/button_layout"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:gravity="center">
        <Button
            android:id="@+id/Button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1"></Button>
        <Button
            android:id="@+id/Button02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"></Button>
        <Button
            android:id="@+id/Button03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3"></Button>
    </LinearLayout>
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/button_layout"
        android:id="@+id/content">
        <ListView
            android:id="@+id/ListView01"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"></ListView>
    </FrameLayout>
</RelativeLayout>

To change how many buttons are shown based on the screen size, you should implement separate layouts for Multiple Screen Sizes.. 要基于屏幕尺寸更改显示的按钮数量,应为“多个屏幕尺寸”实现单独的布局。

http://developer.android.com/guide/practices/screens_support.html http://developer.android.com/guide/practices/screens_support.html

Sounds like you want to create your own custom layout class. 听起来您想创建自己的自定义布局类。 That or just fill a LinearLayout (for instance) until you run out of screen space. 或仅填充LinearLayout(例如),直到屏幕空间用完为止。

If you know the size of your buttons in pixels you could use DisplayMetrics to get the dimensions of the screen then calculate how many buttons will fit in your allotted space. 如果您知道按钮的大小(以像素为单位),则可以使用DisplayMetrics来获取屏幕的尺寸,然后计算在分配的空间中可以容纳多少个按钮。

     DisplayMetrics metrics = new DisplayMetrics();
     getWindowManager().getDefaultDisplay().getMetrics(metrics);

metrics.heightPixels givs absolute height in pixels metrics.ydpi gives exact physical pixels per inch of the screen metrics.heightPixels给出了像素的绝对高度metrics.ydpi给出了每英寸屏幕的确切物理像素

and metrics.density gives logical density for scaling purposes 和metrics.density给出用于扩展目的的逻辑密度

see here: http://developer.android.com/reference/android/util/DisplayMetrics.html 参见此处: http : //developer.android.com/reference/android/util/DisplayMetrics.html

then just do something like 然后做类似的事情

do{
    Button button=new Button(context);
    button.setText("yada yada")
    button.allYoursettings....
    .
    .
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    LinearLayout layout=(LinearLayout) findViewById(R.id.yourlayout);
    layout.addView(button,p);
} while(havespaceleft);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM