【问题标题】:How to make a round button with Image clickable and navigates to another screen如何制作带有可点击图像的圆形按钮并导航到另一个屏幕
【发布时间】:2015-01-21 18:55:23
【问题描述】:

要求:我想设计一个嵌入图像的圆形按钮,如果我单击一次按钮,则图像将更改为另一个图像,并且它应该导航到另一个屏幕。我怎样才能使它成为可能。

在这里我可以做圆形按钮,当点击图像时,它会变成另一个图像。但我的问题是:图像以方形模式显示,但不是圆形按钮。它显示在按钮的边界之外。我需要以圆形方式显示图像,它应该在圆形按钮中。我怎样才能使它成为可能: 这是我的代码:

MainActivity.java:

      package com.example.circularbutton;

        import android.support.v7.app.ActionBarActivity;
     import android.content.Context;
      import android.content.Intent;
       import android.os.Bundle;
         import android.view.Menu;
           import android.view.MenuItem;
     import android.view.View;
       import android.view.View.OnClickListener;
           import android.widget.Button; 

    public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CircleImageView b1=(CircleImageView)  findViewById(R.id.btn);//showing issue here
    b1.setImageResource(R.drawable.my);


        final Context context=this;
        b1.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View arg0) {

            Intent intent = new Intent(context, Hello.class);
                           startActivity(intent);   
            }
            } );
    }

    }




   my *myborder3.xml* it shapes the button

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

         <shape xmlns:android="http://schemas.android.com/apk/res/android">

  <solid android:color="#FFFFFF"/>    

  <stroke android:width="5dp"
    android:color="#000000"
    />

   <padding android:left="45dp"
     android:top="45dp"
     android:right="45dp"
     android:bottom="45dp"
     /> 

 <corners android:bottomRightRadius="55dip" android:bottomLeftRadius="55dip" 
     android:topLeftRadius="55dip" android:topRightRadius="55dip"/> 

       </shape>




   *my.xml* is:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_focused="true" android:drawable="@drawable/mine">
   </item>
     <item android:state_pressed="true"   android:drawable="@drawable/mine1"></item>
 <item android:drawable="@drawable/mine10" ></item>
        </selector>

activiyt_main.xml:

   <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.circularbutton.MainActivity" >


   <de.hdodenhof.circleimageview.CircleImageView
  android:id="@+id/btn"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
   android:background="@drawable/myborder3"
       android:text="Test" />

       </RelativeLayout>


  build.gradile:

      buildscript {
    repositories {
    mavenCentral()
    jcenter()
    }
  dependencies {



      compile de.hdodenhof.circleimageview.CircleImageView //added here


   classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6'
    classpath 'com.android.tools.build:gradle:1.0.0'
  }
   }

       allprojects {
   apply plugin: "eclipse"
   apply plugin: "idea"

   version = '1.0'
    ext {
    appName = 'CircularBUtton'
    gdxVersion = '1.5.3'
    roboVMVersion = '1.0.0-beta-03'
    box2DLightsVersion = '1.3'
    ashleyVersion = '1.3.1'
    aiVersion = '1.4.0'
}

     repositories {
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    maven { url "https://oss.sonatype.org/content/repositories/releases/" }
  }
  }

  project(":desktop") {
apply plugin: "java"


dependencies {
    compile project(":core")
    compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
    compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
    compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
}
}

    project(":android") {
apply plugin: "android"

configurations { natives }

    dependencies {
    compile project(":core")
    compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
    natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
    compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
    natives "com.badlogicgames.gdx:gdx-box2d-   platform:$gdxVersion:natives-armeabi"
    natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
    natives "com.badlogicgames.gdx:gdx-box2d- platform:$gdxVersion:natives-x86"
   }
   }

       project(":html") {
  apply plugin: "gwt"
   apply plugin: "war"


  dependencies {
    compile project(":core")
    compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion"
    compile "com.badlogicgames.gdx:gdx:$gdxVersion:sources"
    compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources"
    compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion:sources"
    compile "com.badlogicgames.gdx:gdx-box2d-gwt:$gdxVersion:sources"
   }
   }

    project(":core") {
      apply plugin: "java"


     dependencies {
    compile "com.badlogicgames.gdx:gdx:$gdxVersion"
    compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
    }
    }

  tasks.eclipse.doLast {
  delete ".project"
    }

我是 android 开发的初学者。请帮帮我。

更清楚:

按钮和图像应该是圆形的,当用户点击它时,它应该在圆形的同一个按钮中显示另一个图像,并且应该导航到另一个屏幕

【问题讨论】:

  • 你可以添加一些输出的截图吗?
  • 您在任何地方都没有按钮点击监听器 - 应用程序如何知道点击按钮时该做什么?
  • 如果我点击按钮,它会变成另一个图像。对不起,我提供了我将进一步提供的听众。但是我怎样才能将图像塑造成圆形(它应该适合圆形按钮)。如果我单击屏幕截图中显示的图像(my.xml 中的 mine10),它会显示另一张图像(my.xml 中的 mine1)1-2 秒,然后再次仅显示第一张图像。
  • 你也可以发布你的按钮 onclicklistener 代码吗?
  • 我认为这对我的要求没有必要。好的,无论如何我都会用监听器更新代码。

标签: android


【解决方案1】:

也许CircleImageView 可能是您正在寻找的。您需要做的就是将此行包含在您应用程序的build.gradle 文件中的dependencies 中:

compile 'de.hdodenhof:circleimageview:1.2.1'

然后在您的xml 布局中使用de.hdodenhof.circleimageview.CircleImageView 而不是普通的ImageView。

更新:如果使用 Eclipse,您将不得不手动导入库。从 github 克隆项目并导入您的工作区。

更新 2:您将视图夸大为 Button,这是错误的。您应该将其转换为 CircleImageView。

CircleImageView b1=(CircleImageView) findViewById(R.id.btn);

您也可以使用setImageResource 而不是设置背景,因为它是ImageView。

b1.setImageResource(R.drawable.my);

【讨论】:

  • 如果可能的话,你能不能把它简单一点。谢谢你。我无法对该视图进行分析。我在学习时遇到了新问题。因为我是 android 的初学者。
  • 我没有在我的应用程序中找到 build.gridle。我在日食中做
  • 我已完成上述更新,但应用程序未启动,而是显示为“应用程序已意外停止”的问题
  • 'CircleImageview cannot be resolved to a type' 显示错误。
  • 那你没有正确导入CircleImageView库作为你项目的依赖。
【解决方案2】:

您可以使用this 小部件。

只需将您在activiyt_main.xml 中的Button 视图替换为以下内容

<at.markushi.ui.CircleButton
    android:layout_width="64dip"
    android:layout_height="64dip"
    android:src="@drawable/ic_action_tick"
    app:cb_color="#99CC00"
    app:cb_pressedRingWidth="8dip" />

【讨论】:

  • 错误:解析 XML 时出错:正在显示未绑定的前缀问题。
【解决方案3】:

如果你看How to make a round button?的答案,xml应该是这样的

<shape
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <solid .../>
    <corners .../>
</shape>

(参见http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape)solid 和corners 应该是shape 的子级。

您也可以尝试使用android:shape="oval"

【讨论】:

  • 谢谢史蒂文。但是我能够创建圆形按钮,也实现了在单击该按钮一次后将一个图像更改为另一个图像。但我的问题是:圆形图像没有显示在圆形按钮内。显示的是实际大小而不是圆形图像。如何以圆形形式制作该图像并且必须将其嵌入圆形按钮中。
  • 你能用你所描述的图像更新问题吗?
  • 你需要进一步解释这个问题。根据该最新图像,这些图像看起来像是正确的圆形图像。问题是在CircleImageView 中它可以正常工作,但在您的应用程序中却不能吗?如果是这样,您可能需要发布更多代码,以便我们了解这两个应用程序中哪些是有效的,哪些是无效的。
【解决方案4】:

CircleImageView 实现 setOnClickListener

例子:

布局:

<de.hdodenhof.circleimageview.CircleImageView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/btn_interior"
                android:layout_width="96dp"
                android:layout_height="96dp"
                android:src="@drawable/interior"
                app:civ_border_width="2dp"
                app:civ_border_color="#FF4081"/>

Java:

CircleImageView b1=(CircleImageView) findViewById(R.id.btn_interior);
    b1.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) {
    Log.i("Debug", "Click en el boton");
    }
});

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 2019-10-01
    • 2010-11-09
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多