简体   繁体   English

安装材料设计支持库

[英]Installing Support library for Material Design

Using SDK Manager I downloaded the Support Library to use Material Design for previous versions. 使用SDK Manager,我下载了支持库以将Material Design用于以前的版本。

Now the question is, how do I use this in my Android Studio project? 现在的问题是,如何在Android Studio项目中使用它? Specifically if I want Material Design? 特别是如果我要材质设计?

Here is my gradle : 这是我的礼物

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "obx.com.futurister"
        minSdkVersion 17
        targetSdkVersion 21
        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:22.2.1'
}

just add this on depedencies 只需在依赖上添加

compile 'com.android.support:design:22.2.1'

hope it help 希望对你有帮助

Android studio 1.4 has many new templates that use the material design support library. Android Studio 1.4具有许多使用材质设计支持库的新模板。 I highly recommend downloading it! 我强烈建议下载它! Otherwise, you can do it as follows: 否则,您可以按照以下步骤进行操作:

build.gradle should contain build.gradle应该包含

compile 'com.android.support:design:22.2.1'
compile 'com.android.support:cardview-v7:22.2.1'
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:recyclerview-v7:22.2.1'

main_activity.xml main_activity.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="company.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main_activity" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_action_send_now" />

</android.support.design.widget.CoordinatorLayout>

content_main_activity.xml content_main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                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"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                tools:context="company.MainActivity"
                tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>

</RelativeLayout>

And in your onCreate method in MainActivity.java 并在MainActivity.java中的onCreate方法中

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);    

        toolbar.setTitle(getTitle());

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

Your imports should include: 您的进口商品应包括:

  • import android.support.design.widget.FloatingActionButton;
  • import android.support.v7.app.AppCompatActivity;
  • import android.support.v7.widget.Toolbar;

And MainActivity should extend AppCompatActivity And your main activity theme in styles.xml should include 并且MainActivity应该扩展AppCompatActivity并且styles.xml中的主要活动主题应该包括

  • <item name="windowActionBar">false</item>
  • <item name="windowNoTitle">true</item>
  • <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  • <item name="android:statusBarColor">@android:color/transparent</item>

Hope that helps! 希望有帮助! Comment if you have a further question! 如果您还有其他问题,请发表评论! Otherwise have a nice day! 否则,祝您有美好的一天!

Add following dependencies to the build.gradle file of your module. 将以下依赖项添加到模块的build.gradle文件中。

compile 'com.android.support:design:22.2.1'
compile 'com.android.support:cardview-v7:22.2.1'
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:recyclerview-v7:22.2.1'

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

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