Android 为小部件设计布局
有人可以指导我如何设计一个小部件的布局,如所附的图片所示Android 为小部件设计布局,android,android-layout,android-widget,Android,Android Layout,Android Widget,有人可以指导我如何设计一个小部件的布局,如所附的图片所示 星号部分将是名人的缩略图像 对角线分隔符“/”上方和下方的部分应具有 分开的听众 有人能告诉我设计这个布局的最佳和有效的方法吗 非常感谢您的帮助 当然有很多可能的解决方案。一个简单的例子是: 利用标准列表视图 让自定义适配器(为此扩展BaseAdapter)填充每一行 列表项的布局应使用水平方向的线性布局 仅为“/”分隔符创建自定义小部件 在列表项的布局中,放置一个ImageView(用于celeb图片)并添加为所需自定义“/”小部件的
当然有很多可能的解决方案。一个简单的例子是:
- 利用标准列表视图
- 让自定义适配器(为此扩展BaseAdapter)填充每一行
- 列表项的布局应使用水平方向的线性布局
- 仅为“/”分隔符创建自定义小部件
- 在列表项的布局中,放置一个ImageView(用于celeb图片)并添加为所需自定义“/”小部件的菜单
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。所以请告诉我更多,我会更新我的答案:-)