简体   繁体   English

您如何在Eclipse Android开发中使用自定义视图?

[英]How do you use custom views in Eclipse Android development?

I have downloaded a custom view and I want to implement it in my project but here's my dilemma: People use big terms or imply an action that I have never done before and have no knowledge of. 我已经下载了一个自定义视图,并想在我的项目中实现它,但这是我的两难选择:人们使用大词或暗示我从未做过且不了解的动作。

This is the view I am trying to download: http://androidcustomviews.com/holocolorpicker/ 这是我要下载的视图: http : //androidcustomviews.com/holocolorpicker/

My questions are: 我的问题是:

  1. How do I import a view into an existing Eclipse project? 如何将视图导入到现有的Eclipse项目中?

  2. Which gradle file do I modify? 我要修改哪个gradle文件? There are 5 for me (build.gradle, gradle.properties, gradlew.file, gradlew.bat, settings.gradle). 对我来说有5个(build.gradle,gradle.properties,gradlew.file,gradlew.bat,settings.gradle)。

  3. Why do you have to add 'compile' to the gradle? 为什么必须在gradle中添加“ compile”? Isn't this something the developer could just write in it? 这不是开发人员可以在其中写的东西吗?

How do I import a view into an existing Eclipse project? 如何将视图导入到现有的Eclipse项目中?

It is not so much "importing a view" as it is importing the library that will let you use the view, which is explained below. 与其说是“导入视图”,还不如说它是导入使您可以使用该视图的库,下面将对此进行说明。

Which gradle file do I modify? 我要修改哪个gradle文件? There are 5 for me (build.gradle, gradle.properties, gradlew.file, gradlew.bat, settings.gradle). 对我来说有5个(build.gradle,gradle.properties,gradlew.file,gradlew.bat,settings.gradle)。

One of the build.gradle files. build.gradle文件之一。 One will look somewhat like this 一个会看起来像这样

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0-beta1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

that is not the right one as the comment suggest. 这不是评论所建议的正确方法。 You need to add it to the other one, which looks somewhat like this 您需要将其添加到另一个,看起来像这样

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "xxxx"
        minSdkVersion 23
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.larswerkman:HoloColorPicker:1.5'
}

See I added it at the bottom along with the other dependencies. 请参阅我在底部将其与其他依赖项一起添加。

Now when you do a gradle sync, the library will be downloaded and automatically included in your project. 现在,当您执行gradle同步时,该库将被下载并自动包含在您的项目中。

After that you can use the view in your xml file like any other view 之后,您可以像其他任何视图一样使用xml文件中的视图

With a regular view you use : 在常规视图中,您可以使用:

<View
  ..
  .. />

Now with this custom view you use: 现在,使用此自定义视图可以使用:

<com.larswerkman.holocolorpicker.ColorPicker
  ..
  .. />

Why do you have to add 'compile' to the gradle? 为什么必须在gradle中添加“ compile”? Isn't this something the developer could just write in it? 这不是开发人员可以在其中写的东西吗?

I'm not able to answer that, because it is a choice of the gradle development team. 我无法回答,因为这是gradle开发团队的选择。 But basically with compile you say "here I have this library, please compile it into the project for me" 但是基本上在compile您会说“这里有这个库,请帮我编译到项目中”

Yes, you need to add dependency to gradle. 是的,您需要添加依赖关系到gradle。

dependencies {
    compile 'com.larswerkman:HoloColorPicker:1.5'
}

After that, you can use this color picker in your layout, just adding like a simple view. 之后,您可以在布局中使用该颜色选择器,就像添加一个简单视图一样。

<com.larswerkman.holocolorpicker.ColorPicker
    android:id="@+id/picker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM