【问题标题】:How to include subfolder layout with <include/> tag android如何使用 <include/> 标签包含子文件夹布局
【发布时间】:2019-01-19 19:25:26
【问题描述】:

我已经在这个论坛上搜索文章和搜索问题都只显示“如何设置 gradle 子目录”

我已经设置了build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        applicationId "com.ardyfeb.kolabs"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
          res.srcDirs = [
               'src/main/res/layout/partials',
               'src/main/res/layout',
               'src/main/res'
          ]
        }
    }
}

dependencies {
    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.android.support:multidex:+'
    compile 'com.android.support:cardview-v7:+'
    compile 'com.android.support:design:+'
    compile 'com.android.support:support-v13:+'
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.android.support:support-v4:+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

我的目录树

src/main/res
      -- layout
           -  main.xml
           -- partials
               - header.xml

我的问题是: 如何包含 partials/header.xml ?带标签

例子:

<include
        layout="@layout/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/header"/>

【问题讨论】:

  • 包括 layout="@layout/header" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/header"/>
  • 错误显示“找不到资源”
  • 你能给我举个例子吗?
  • 在 layout/main.xml 我想包含 layout/partials/header.xml
  • 标签包含

标签: android xml android-layout android-studio xml-parsing


【解决方案1】:
<include
        android:id="@+id/tagOne"
        layout="@layout/header"
        ...
        />

<include
        android:id="@+id/tagTwo"
        layout="@layout/header"
        ...
        />

示例

如果你有TextView,那么你可以通过标签找到视图

TextView headerOneText = findViewById(R.id.tagOne).findViewById(R.id.textView);

TextView headerTwoText = findViewById(R.id.tagTwo).findViewById(R.id.textView);

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    相关资源
    最近更新 更多