简体   繁体   中英

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/

My questions are:

  1. How do I import a view into an existing Eclipse project?

  2. Which gradle file do I modify? There are 5 for me (build.gradle, gradle.properties, gradlew.file, gradlew.bat, settings.gradle).

  3. Why do you have to add 'compile' to the gradle? Isn't this something the developer could just write in it?

How do I import a view into an existing Eclipse project?

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? There are 5 for me (build.gradle, gradle.properties, gradlew.file, gradlew.bat, settings.gradle).

One of the build.gradle files. 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.

After that you can use the view in your xml file like any other view

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? 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. But basically with compile you say "here I have this library, please compile it into the project for me"

Yes, you need to add dependency to 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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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