简体   繁体   English

Android-如何使按钮的TableLayout适合不同尺寸的屏幕

[英]Android - how to make a TableLayout of buttons fit differently sized screens

I'm developing an Android app using fragments. 我正在使用片段开发Android应用程序。 In two fragments I use a TableLayout to programmatically create a matrix view consisting of buttons. 在两个片段中,我使用TableLayout以编程方式创建由按钮组成的矩阵视图。 The dimension of the matrix differs for different runs of the app, but typically I have 10-15 rows and 10-15 columns. 矩阵的尺寸因应用程序的不同运行而不同,但通常我有10-15行和10-15列。

I have OddsButton extend Button. 我有OddsButton扩展按钮。

I would like to have this view fill the screen on different devices, but I haven't been able to understand what is the right approach. 我想让此视图显示在不同设备上的屏幕上,但是我无法理解什么是正确的方法。 I can (by hardcoding values for width and height) impact the height and width of the buttons (see below for code), but I'm struggling with fitting it to a Nexus 7 anyway. 我可以(通过对宽度和高度的值进行硬编码)来影响按钮的高度和宽度(有关代码,请参见下文),但是无论如何我都在努力使其适合Nexus 7。 If I first find the right height to use to make it fit the screen then when I start adding more width the buttons get a bit higher as well and there is no way for me to adjust that back. 如果我首先找到合适的高度以使其适合屏幕,那么当我开始增加宽度时,按钮也将变得更高,并且我无法调整该高度。

Sorry if this question is strange, but I hope that someone can make sense of this. 抱歉,这个问题很奇怪,但我希望有人能理解。 I realize that I might be trying the totally wrong approach for this. 我意识到我可能为此尝试了完全错误的方法。

    for (int k=1;k<=n2;k=k+1){
        OddsButton b = new OddsButton(this.getActivity());

        b.setLeg1(no);
        b.setLeg2(k);
        b.setO(this.getO(no,k));
        b.setIndex(this.IndexForCombination(no, k));
        b.setMinimumHeight(height);
        b.setHeight(height);
        b.setMinimumWidth(width);
        b.setMinimumHeight(width);
        b.setPadding(0, 0, 0, 0);

Table Layout is a bit tricky. 表布局有点棘手。 Please have a look at the example: 请看下面的例子:

<TableLayout
    android:id="@+id/myId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="*"
    android:weightSum="2"
    >
        <TableRow >
        <TextView 
            android:id="@+id/text1"
            android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
            android:text="bla1"
            /> 
        <TextView 
            android:id="@+id/text2"
            android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
            android:text="bla2"
            /> 
    </TableRow>
     </TableLayout

What is tricky is to first set android:stretchColumns="*" for layout (means stretch all columns) and next android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" (don't ask me why it wors like that, I will never understand some tricky things in Android, but good they work). 棘手的是首先设置android:stretchColumns =“ *”进行布局(意味着拉伸所有列),然后设置android:layout_width =“ 0dp” android:layout_height =“ wrap_content” android:layout_weight =“ 1”(不要问我为什么会这样担心,但我永远都不会理解Android中的一些棘手的东西,但是很好用。 Btw, weightSum is simply the sum of weight of elements you want to have in one row. 顺便说一句,weightSum只是您要在一行中拥有的元素的权重之和。

Sure you can have more tablerows, just the number of elements in each should be the same. 确保可以有更多的表行,只是每个表中的元素数应该相同。

(PS please don't forget to give points if you like the answer ;)). (PS,如果您喜欢答案,请不要忘了给点;)。

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

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