【问题标题】:How to dynamically set button in a tabular form如何以表格形式动态设置按钮
【发布时间】:2015-12-08 20:43:06
【问题描述】:

我正在为我的活动制作布局并感到困惑,我正在考虑制作灵活的布局,让我的视图根据屏幕获得宽度和高度。请阅读以下案例

我想要什么: 这对我来说是最棘手和最困难的部分。

  1. 我需要将 37 个按钮放在屏幕上,这样每一行 获取 3 按钮,然后切换到新行。然而最后一行得到一个或两个按钮。那么在3的情况下 每行中的按钮在最后一行中只产生一个按钮,但那是 好的
  2. 每一行中的每个按钮都应该有不同的 id ,有不同的 背景图片并以不同的方式打开不同的活动 意图附加值。
  3. 行中的按钮必须具有相同的大小,以使其看起来不错。

所以这三点让我很难过。此外,这点中的第二点要重要得多。

请告诉我如何实现这一点我已经阅读了有关网格布局、网格视图、列表视图、表格布局的信息,并且我已经使用了很多次。但我不知道如何将它们用于此特定目的。

注意: 行中的按钮应根据屏幕获得相同的宽度和高度,并且布局应适合所有设备,因此我们应避免硬编码值。

【问题讨论】:

    标签: android android-layout layout


    【解决方案1】:
    1. 只需使用网格视图。将 Max 列设置为 3 和

    2. 使用扩展 Base 适配器的自定义适配器来设置背景并在项目点击侦听器上返回不同的 id。

    3. 使用屏幕尺寸动态设置膨胀项目视图的布局参数。

    【讨论】:

    • 太好了,我已经知道了,我已经实现了所有的东西,但是想知道如何实现点击监听器,但是现在显示并且看起来很酷
    • 请给我一个源代码\
    【解决方案2】:

    我认为您应该看到这些链接,link 1link2。 gridview 是您的最佳选择,因为这些布局会调整大小,您可以轻松地在其适配器中处理它们。

    【讨论】:

    • 我已经尝试了其他一些方法,但没有得到解决方案]
    【解决方案3】:

    使用线性布局代替..使用嵌套概念。就像

    <LinearLayout>
    
       <LinearLayout>
         .
         .
       And So on..
         .
         .
         .
       </LinearLayout>
    
    </LinearLayout>
    

    例如

    ----Outer (Horizontal) layout-----
    |                                |
    |  ---Inner (Vertical) layout-   |
    |  |       [Textview]        |   |
    |  |       [Button]          |   |
    |  |       [Button]          |   |
    |  |       [Button]          |   |
    |  ---------------------------   |
    ----------------------------------
    

    也试试下面的例子

    <LinearLayout android:layout_width="match_parent"
     android:layout_weight="1" 
     android:layout_height="wrap_content"
     android:orientation="vertical">
     <LinearLayout android:layout_height="wrap_content"
     android:layout_width="match_parent">
     <TextView android:text="One,One" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" />
     <TextView android:text="One,Two" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" 
     android:layout_weight="1" />
     <TextView android:text="One,Three" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" />
     <TextView android:text="One,Four" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" />
     </LinearLayout>
     <LinearLayout android:layout_height="wrap_content"
     android:layout_width="match_parent">
     <TextView android:text="Two,One" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" />
     <TextView android:text="Two,Two" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" 
     android:layout_weight="1" />
     <TextView android:text="Two,Three" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" />
     <TextView android:text="Two,Four" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" />
     </LinearLayout>
     <LinearLayout android:layout_height="wrap_content"
     android:layout_width="match_parent">
     <TextView android:text="Three,One" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" />
     <TextView android:text="Three,Two" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" 
     android:layout_weight="1" />
     <TextView android:text="Three,Three" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" />
     <TextView android:text="Three,Four" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" />
     </LinearLayout>
     <LinearLayout android:layout_height="wrap_content"
     android:layout_width="match_parent">
     <TextView android:text="Four,One" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" />
     <TextView android:text="Four,Two" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" 
     android:layout_weight="1" />
     <TextView android:text="Four,Three" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" />
     <TextView android:text="Four,Four" 
     android:layout_height="wrap_content"
     android:layout_width="wrap_content" />
     </LinearLayout>
    </LinearLayout>
    

    输出

    参考链接: Nested Linear Layout, Nested Example

    更新 1 正如你所说,37 个按钮必须在具有不同 id、intent extras 等的应用程序上实现。我认为这是一项简单的工作。为每个按钮设置不同的 id 并为按钮编写一个 onClickListener。使用 switch/If case 来区分每个按钮。

    示例:

    public void buttonOnClick(View view)
    {
     switch(view.getId())
     {
      case R.id.button1:
      // Code for button 1 click
      break;
    
      case R.id.button2:
      // Code for button 2 click
      break;
    
      case R.id.button3:
      // Code for button 3 click
      break;
     }
    }
    

    更多信息请参考此链接:

    OnClickevent for buttons

    谢谢

    【讨论】:

    • 但这不是我想要的
    猜你喜欢
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 2020-07-17
    • 2011-06-01
    • 2019-03-25
    • 2012-07-29
    • 1970-01-01
    • 2013-12-27
    相关资源
    最近更新 更多