【问题标题】:Designing a layout for a widget为小部件设计布局
【发布时间】:2013-07-17 11:28:00
【问题描述】:

有人可以指导我如何设计小部件的布局,如附图所示

  1. 加星标的部分是名人的缩略图
  2. 对角线分隔符“/”上方和下方的部分应具有 单独的侦听器。

有人可以告诉我设计此布局的最佳和有效方法吗?

非常感谢任何帮助。

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    当然有很多可能的解决方案。一个简单的方法是:

    • 使用标准的 ListView
    • 让自定义适配器(为此目的扩展 BaseAdapter)填充每一行
    • 列表项的布局应使用水平方向的 LinearLayout
    • 仅为“/”分隔线创建自定义小部件
    • 在列表项的布局中放置一个 ImageView(用于名人图片)并添加为您需要的自定义“/”小部件的菜单

    对于您的自定义小部件,我建议扩展现有的 Android 小部件,而不是从头开始构建一个。扩展 FrameLayout 可能是一个很好的解决方案,因为您可以拥有一个由三角形覆盖层覆盖的矩形背景。使用 onTouchListener,您可以检测其中的哪些被点击。

    通过使用尽可能多的标准解决方案,可以最大限度地减少创建此类小部件的工作量。

    这是您的自定义小部件的一个抽象示例实现:

        public class DividedView extends FrameLayout implements OnTouchListener {
              public void onCreate(Context context, AttributeSet attr){
                  View firstView =  createFirstView();
    
                  View secondView = createSecondView(); //this view has a triangle shape but with a transparent buttom-right corner... but the boundaries match the complete size of this custom widget... therefore this view consumes all touch events
                  secondView.setOnTouchListener(this);
    
                  addView(firstView);
                  addView(secondView);
              }
    
              ...
    
              public boolean onTouch(MotionEvent event){
                  switch(event.getAction(){
                       case MotionEvent.ACTION_DOWN:
                        //detect if coordinates of touch down are in boundaries of first or second view... if yes trigger click event for firstView or secondView depending on coordinates
                        break;
                  }
              }
    
              //via this method you can set corresponding click listener for each of the divided views
              public void setFirstViewOnClickListener(OnClickListener onClickListener)
              ...
    
        }
    

    【讨论】:

    • 扩展 FrameLayout 可能是一个很好的解决方案,因为您可以拥有一个由三角形覆盖层覆盖的矩形背景而不是这样做java代码,我可以在xml中定义这个并在java代码中使用它吗?
    • 请告诉我更多您想要实现的行为。当然,可以在 XML 中设计一切并在您的代码中引用它。例如,在上面的示例中,我没有进一步指定方法 createFirstView()、createSecondView()。在这些方法中,您当然可以从 XML 扩展具体布局。当您只需要区分用户点击左上角或右下角时,您可以在 XML 中设计所有内容,只需要分配 onTouchListener 来区分点击事件。所以请告诉我更多信息,我会更新我的答案:-)
    猜你喜欢
    • 2013-08-19
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 2021-01-12
    相关资源
    最近更新 更多