【问题标题】:Images on buttons and keep proportions of buttons?按钮上的图像并保持按钮的比例?
【发布时间】:2015-09-17 09:03:16
【问题描述】:

我正在将这款扫雷游戏作为一个应用程序作为一个项目来制作。我已经把它全部编码了,所以它工作得很好,现在我想在按钮上添加图像,以表示地雷、旗帜等。

我所做的所有研究都指出,要么将图像作为按钮背景,要么使用图像按钮。当我使用这两种方法中的任何一种时,它都会使按钮偏离其原始比例并使其覆盖屏幕。

如何将图像设置到按钮上但不更改按钮的当前尺寸?

仅供参考:我正在使用 android studio。

这是我的 xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.daniel.msb.Small"
android:background="#ff666666">

<TableRow
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:id="@+id/Header"
android:background="#ff898989"
android:layout_gravity="center">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="0/0"
    android:id="@+id/txtCount"
    android:layout_weight="0.1"
    android:layout_gravity="center"
    android:background="#ff87141d"
    android:gravity="center" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Small"
    android:id="@+id/txtHeader"
    android:layout_weight="0.5"
    android:gravity="center|center_horizontal"
    android:layout_gravity="center"
    android:textSize="30dp" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:text="Flag"
    android:id="@+id/chbFlag"
    android:layout_gravity="center"
    android:layout_weight="0.1"
    android:layout_span="0"
    android:layout_column="0" />
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/Row1">

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button1_1"
    android:layout_weight="1"
    android:clickable="true"
    android:onClick="mineCheck"
    android:longClickable="false" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button1_2"
    android:onClick="mineCheck"
    android:layout_weight="1"/>

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button1_3"
    android:onClick="mineCheck"
    android:layout_weight="1"
    android:clickable="true"
    android:longClickable="false" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button1_4"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button1_5"
    android:onClick="mineCheck"
    android:layout_weight="1" />
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/Row2">

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button2_1"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button2_2"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button2_3"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button2_4"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button2_5"
    android:onClick="mineCheck"
    android:layout_weight="1" />
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/Row3">

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button3_1"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button3_2"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button3_3"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button3_4"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button3_5"
    android:onClick="mineCheck"
    android:layout_weight="1" />
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/Row4">

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button4_1"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button4_2"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button4_3"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button4_4"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button4_5"
    android:onClick="mineCheck"
    android:layout_weight="1" />
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/Row5">

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button5_1"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button5_2"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button5_3"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button5_4"
    android:onClick="mineCheck"
    android:layout_weight="1" />

<Button
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:text=""
    android:id="@+id/button5_5"
    android:onClick="mineCheck"
    android:layout_weight="1" />
</TableRow>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Play again?"
    android:id="@+id/button"
    android:layout_weight="0"
    android:onClick="restart" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Return to main menu"
    android:id="@+id/btnReturn"
    android:layout_weight="0"
    android:onClick="returnToMain" />

</TableLayout>

【问题讨论】:

    标签: android


    【解决方案1】:

    我认为你应该通过这些:

    1.Android: combining text & image on a Button or ImageButton

    2.Android image button

    他们已经清楚地解释了如何在按钮上使用图像。如果出现任何问题,请再次访问。

    【讨论】:

      【解决方案2】:

      确实有很多方法可以实现这一点。您可以将 TextView 替换为 ImageView 并根据需要定义它 ScaleType 。您可以在 XML 中定义位图并将其设置为适合您的 TextView 的尺寸,然后将其用作背景。

      有更多的方法,但最简单的就是替换为 ImageView。

      【讨论】:

        【解决方案3】:

        也许您可以调整图像的大小以使其适合您的按钮大小,这样您就可以模拟“填充”效果而不会弄乱原始大小。

        Here some info

        【讨论】:

          【解决方案4】:

          检查这里: http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

          您可以通过为layout_widthlayout_height分配一个数字来将宽度和高度设置为固定大小。

          您可能希望使用 sp 单位,以便按钮仍会根据屏幕大小改变大小。

          【讨论】:

            【解决方案5】:

            您可以尝试使用ImageViews (see here) 代替按钮并将事件处理程序直接附加到ImageViews。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-01-24
              • 2011-06-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-09-11
              • 2015-06-03
              相关资源
              最近更新 更多