【问题标题】:How to make this UI with imagebuttons?如何使用 imagebuttons 制作这个 UI?
【发布时间】:2011-08-11 03:54:27
【问题描述】:

我想创建这样的主 UI: http://lh3.ggpht.com/RYB2ckycDc_4rb2bmsDzqLLmYcj37xDOA7d7sR6XIVfwRb3a44_HqMqfb1vdmtxTEidenVY4C9RcTREl

是否可以使用 ImageButton 做到这一点?图像必须具有相同的分辨率? 非常感谢。

XML文件,一行图片按钮:

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

<RelativeLayout android:id="@+id/acceuil"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:orientation="vertical"
   xmlns:android="http://schemas.android.com/apk/res/android">


<ImageButton

    android:id="@+id/consom" 

    android:layout_alignParentLeft="true"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/consom_icon">
</ImageButton>
<ImageButton

    android:id="@+id/param" 

    android:layout_alignParentRight="true"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
     android:src="@drawable/consom_icon" >
 </ImageButton>

 </RelativeLayout>

这是我所拥有的: http://imageshack.us/f/856/imagebutton.png/

【问题讨论】:

  • 我不明白为什么这不可能。到目前为止你尝试了什么,你在哪里被阻止了?

标签: android imagebutton


【解决方案1】:

使用 GridView,您可以使用 res/ 文件夹中 hdpi、mdpi 和 ldpi 中的一组图标来填充它。填充列表时,请确保提供图标和 ID,以便确定单击了哪个按钮来启动该按钮应该完成的活动/意图。

Android 有一个关于我所说的要点的可靠示例。 http://developer.android.com/resources/tutorials/views/hello-gridview.html

关于如何使用 switch 语句处理点击的示例:

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new MyGridViewAdapter(this));
    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            switch (v.getId()) {
            case R.drawable.my_button:
                startActivity(new Intent("com.awesome.Activity"));
            }
        }
    });

【讨论】:

  • 我尝试使用 GridView 但不知道如何在点击图像时进行处理(启动活动)。这个例子向您展示了如何只创建一个 GridView,而不是如何对其进行一些处理。
  • stackoverflow.com/questions/7012961/… 这是我创建的主题。
  • 这是一个不同的问题。该示例准确地向您展示了如何检测单击了哪个项目(单击项目时它会生成一个 Toast)。我已经用您正在寻找的示例更新了我的帖子。
  • 如果我想触发 carburant.java 类,最后一行应该是这样的? startActivity(new Intent(carburant.class)); ???因为我有一个错误
  • 错误是什么?您是否有要在清单中声明的​​活动?尝试使用 new Intent("com.package.anotherpackage.MyActivity") 开始您的活动
【解决方案2】:

你有没有这样尝试过,

android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">

android:orientation="horizo​​ntal ......>

android:id="@+id/consom" 
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/consom_icon">

图像按钮>

android:id="@+id/param" 
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
 android:src="@drawable/consom_icon" >

图像按钮>

线性布局>

android:orientation="horizo​​ntal ......>

android:id="@+id/consom" 
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/consom_icon">

图像按钮>

android:id="@+id/param" 
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
 android:src="@drawable/consom_icon" >

图像按钮>

线性布局>

【讨论】:

    【解决方案3】:

    您可以使用 TableLayout 中的 ImageButtons 来制作像这样的“仪表板”类型的 UI,但我认为如果您希望部分图像显示在按钮的边缘上方,您会遇到问题。你可能会更好地使用 ImageViews,它有一个按钮类型的图像,上面有另一个图像。

    查看 android UI 设计模式,他们在其中讨论类似 twitter 的应用设计。

    编辑: 尝试类似下面的 TableLayout

    <TableLayout>
     <TableRow>
      <ImageButton />
      <ImageButton />
     </TableRow>
     <TableRow>
      <ImageButton />
      <ImageButton />
     </TableRow> 
    </TableLayout>
    

    【讨论】:

    • 完全正确。我正在使用 ImageButons,但不知道如何调整每个图像按钮以向左移动,第二个以向右移动,在底部向左移动......等等......我制作了一个 XML 文件。请看我的第一篇文章
    • gridview 目前运行良好。谢谢你的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2023-03-13
    • 2020-09-17
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2018-09-21
    相关资源
    最近更新 更多