【问题标题】:Multi Tab application多标签应用程序
【发布时间】:2020-07-05 12:57:21
【问题描述】:

我在 android 中创建了多标签应用程序,但我无法在图片中的类中引用我的 Xml 元素,我在这里尝试了解决方案 findViewById in Fragment 但它对我不起作用

这是我的 xml 代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CountDown">


    <EditText
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="101dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="101dp"
        android:layout_marginBottom="450dp"
        android:autofillHints="time will be here"
        android:inputType="time"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.482"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.45"
        tools:ignore="Autofill,LabelFor"
        tools:text="time" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的主要活动课

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
        ViewPager viewPager = findViewById(R.id.view_pager);
        viewPager.setAdapter(sectionsPagerAdapter);
        TabLayout tabs = findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);
    }
}

这是我的主要活动 xml

    <androidx.coordinatorlayout.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"
        tools:context=".MainActivity">
    
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:minHeight="?actionBarSize"
                android:padding="@dimen/appbar_padding"
                android:text="@string/app_name"
                android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
    
            <com.google.android.material.tabs.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary" />
        </com.google.android.material.appbar.AppBarLayout>
    
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

  • 除了代码图片之外,我们是否建议在问题正文中添加文本代码。
  • 为了提高您获得良好响应的机会,我们建议您像在之前的帖子中那样以文本形式发布代码。图像难以阅读
  • @abdelrahman,感谢您更新帖子。看起来你有一个潜在的好答案

标签: java android xml


【解决方案1】:

试试下面的代码。

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
   View frag = inflater.inflate(R.layout.count_down, container, false);
   EditText time = frag.findViewById(R.id.time);
   return frag;
}

【讨论】:

  • 当我用你上面写的相同样式编写代码时没有任何错误
  • 你能 github 上你的代码吗?如果你这样做,我可以帮助你。
  • github.com/AbdelrahmanHamoda/Timer 在这里,但还没有包括一些 ui 更改
  • 根据 mainactivty,您从未调用过倒计时或秒表片段
  • 这只是根布局而不是片段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多