【问题标题】:Installing Support library for Material Design为 Material Design 安装支持库
【发布时间】:2015-10-08 14:13:11
【问题描述】:

使用 SDK 管理器,我下载了支持库以使用以前版本的 Material Design。

现在的问题是,如何在我的 Android Studio 项目中使用它?特别是如果我想要 Material Design?

这是我的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'
}

【问题讨论】:

    标签: android android-gradle-plugin material-design


    【解决方案1】:

    将以下依赖项添加到模块的 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'
    

    【讨论】:

    • 谢谢,这样可以使用所有材料设计功能吗? :)
    • 是的。这将启用 Support 库支持的所有 Material Design 功能。请通过this博客
    • @silwar cardview 不需要。
    • 可能23.0.1比22.2.1好
    【解决方案2】:

    只需将其添加到依赖项

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

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      Android studio 1.4 有许多使用材料设计支持库的新模板。我强烈推荐下载它!否则,你可以这样做:

      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

      <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

      <?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>
      

      在 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) {
      
                  }
              });
          }
      

      您的导入应包括:

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

      并且 MainActivity 应该扩展 AppCompatActivity 您在 styles.xml 中的主要活动主题应包括

      • &lt;item name="windowActionBar"&gt;false&lt;/item&gt;
      • &lt;item name="windowNoTitle"&gt;true&lt;/item&gt;
      • &lt;item name="android:windowDrawsSystemBarBackgrounds"&gt;true&lt;/item&gt;
      • &lt;item name="android:statusBarColor"&gt;@android:color/transparent&lt;/item&gt;

      希望对您有所帮助!如果您还有其他问题,请发表评论!否则祝你有美好的一天!

      【讨论】:

      • 您应该使用 v23.0.1。设计库也足够了。
      • 哦?它包含所有其他三个吗?同样是的,v23.0.1 如果它是为您的应用程序配置的,但我的不是,所以这只是我复制和粘贴的内容。所以对于某些人来说是的,否则22.2.1 就足够了。
      • 它与 Appcompat 有依赖关系。
      • 好的,所以它不包括卡片视图或回收器视图?
      • 好吧,不管怎样,我不同意,因为它是 with material design 发布的,但这并不重要,所以谢谢。
      猜你喜欢
      • 1970-01-01
      • 2015-12-04
      • 2020-09-30
      • 1970-01-01
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      相关资源
      最近更新 更多