我试图在我的项目中使用 TabLayout 但我无法在我的项目中导入 TabLayout 类请帮助我。这是代码。
构建.gradle
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation "com.google.android.material:material:1.0.0"
Run Code Online (Sandbox Code Playgroud)
}
我还通过引用上一个问题添加了 android.material:material:1.0.0" ,但它对我不起作用。
这是我的 xml 文件。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/white_grey_border_top">
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
实际上,我正在学习 youtube 上的教程,它对他有用,但对我不起作用。
这是我的java类。
package Home;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TabLayout; //TabLayout showing red color.`enter code
here`
import com.example.footag.R;
public class HomeActivity extends AppCompatActivity {
private static final String TAG = "HomeActivity";
private static final int ACTIVITY_NUM = 0;
private Context mContext = HomeActivity.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
setupViewPager();
}
private void setupViewPager(){
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new CameraFragment());
adapter.addFragment(new HomeFragment());
adapter.addFragment(new ProfileFragment());
ViewPager viewPager = (ViewPager) findViewById(R.id.container);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);// TabLayout showing red color.
tabLayout.setupWithViewPager(viewPager);
}
Run Code Online (Sandbox Code Playgroud)
}
我建议您执行 2 个步骤。
步骤 1. 迁移到新的 AndroidX 库,而不是使用 AppCompat 库。使用重构?迁移到 AndroidX。它将用 AndroidX 类似物替换您的依赖项。
步骤 2. 使用com.google.android.material:material:1.0.0依赖获取com.google.android.material.tabs.TabLayout类。在您的 xml 和 java 文件中使用此类。的包名将ViewPager是androidx.viewpager.widget.
从 AndroidX 库导入的 xml 示例:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/white_grey_border_top">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5359 次 |
| 最近记录: |