Android 在表格布局上显示对话框片段
我试图全屏显示DialogFragment,这样ActionBar仍然可见,但TableOut中的选项卡被隐藏 左边的图像,就是我成功实现的目标;右边的图像是我的目标: 有两个问题:Android 在表格布局上显示对话框片段,android,android-fragments,android-design-library,android-tablayout,Android,Android Fragments,Android Design Library,Android Tablayout,我试图全屏显示DialogFragment,这样ActionBar仍然可见,但TableOut中的选项卡被隐藏 左边的图像,就是我成功实现的目标;右边的图像是我的目标: 有两个问题: 选项卡仍然显示,用户可以与之交互 由于显示对话框的额外FrameLayout,因此ViewPager内容仍然可见(FAB按钮不是对话框的一部分)。这也意味着用户可以与寻呼机中的内容交互,其中包括更改选项卡的功能 主要活动布局(Main.xml) MainActivity.java package com.ex
FrameLayout
,因此ViewPager
内容仍然可见(FAB按钮不是对话框的一部分)。这也意味着用户可以与寻呼机中的内容交互,其中包括更改选项卡的功能
MainActivity.java
package com.example.app;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class MainActivity
extends AppCompatActivity
{
private static final String TAG = "MainActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Setup AppBar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
// Setup ViewPager
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}
// Setup TabLayout
TabLayout tl = (TabLayout) findViewById(R.id.tab_layout);
tl.setupWithViewPager(viewPager);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(
getSupportFragmentManager(), MainActivity.this);
adapter.addFragment(
new Fragment1(),
"Tab 1", R.drawable.numeric_1_box_outline);
adapter.addFragment(
new Fragment2(),
"Tab 2", R.drawable.numeric_2_box_outline);
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<Integer> mFragmentIcons = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
private Context context;
public Adapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
public void addFragment(Fragment fragment, String title, int iconId) {
mFragments.add(fragment);
mFragmentTitles.add(title);
mFragmentIcons.add(iconId);
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
Drawable image = context.getResources().getDrawable(
mFragmentIcons.get(position), null);
image.setBounds(0, 0, image.getIntrinsicWidth(),
image.getIntrinsicHeight());
SpannableString sb = new SpannableString(" " + mFragmentTitles.get(position));
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
}
}
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setup AppBar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
// Setup ViewPager
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}
// Setup TabLayout
TabLayout tl = (TabLayout) findViewById(R.id.tab_layout);
tl.setupWithViewPager(viewPager);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container, new DialogFragment());
transaction.addToBackStack("tag");
transaction.commit();
}
...
public class DialogFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dialog, container, false);
final Toolbar toolbar = (Toolbar) view.findViewById(R.id.fragment_toolbar);
toolbar.setTitle("Dialog Title");
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().getSupportFragmentManager().popBackStack();
}
});
return view;
}
}
package com.example.app;
导入android.content.Context;
导入android.graphics.drawable.drawable;
导入android.os.Bundle;
导入android.support.design.widget.TabLayout;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentPagerAdapter;
导入android.support.v4.view.ViewPager;
导入android.support.v7.app.ActionBar;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.text.Spannable;
导入android.text.SpannableString;
导入android.text.style.ImageSpan;
导入android.util.Log;
导入java.util.ArrayList;
导入java.util.List;
公开课活动
扩展AppCompative活动
{
私有静态最终字符串TAG=“MainActivity”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//设置应用程序栏
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
如果(工具栏!=null){
设置支持操作栏(工具栏);
}
//设置视图寻呼机
ViewPager ViewPager=(ViewPager)findViewById(R.id.ViewPager);
if(viewPager!=null){
设置viewPager(viewPager);
}
//设置表格布局
TabLayout tl=(TabLayout)findViewById(R.id.tab_布局);
tl.setupWithViewPager(viewPager);
}
@凌驾
public void onBackPressed(){
super.onBackPressed();
}
专用无效设置ViewPager(ViewPager ViewPager){
适配器=新适配器(
getSupportFragmentManager(),MainActivity.this);
adapter.addFragment(
新片段1(),
“表1”,R.可绘制数字框(轮廓);
adapter.addFragment(
新片段2(),
“表2”,R.可绘制数字框(轮廓);
viewPager.setAdapter(适配器);
}
静态类适配器扩展FragmentPagerAdapter{
私有最终列表MFFragments=new ArrayList();
私有最终列表MFFragmentications=new ArrayList();
私有最终列表MFFragmentTitles=new ArrayList();
私人语境;
公共适配器(FragmentManager fm,上下文){
超级(fm);
this.context=上下文;
}
public void addFragment(片段片段、字符串标题、int-iconId){
添加(片段);
MFFragmentTitles.add(标题);
添加(iconId);
}
@凌驾
公共片段getItem(int位置){
返回mFragments.get(位置);
}
@凌驾
public int getCount(){
返回mffragments.size();
}
@凌驾
公共字符序列getPageTitle(int位置){
Drawable image=context.getResources().getDrawable(
获取(位置),null);
image.setBounds(0,0,image.getIntrinsicWidth(),
getIntrinsicHeight());
SpannableString sb=新的SpannableString(“+MFFragmentTitles.get(position));
ImageSpan ImageSpan=新的ImageSpan(图像,ImageSpan.ALIGN_-BOTTOM);
sb.setSpan(图像SPAN,0,1,SPANABLE.SPAN_EXCLUSIVE_EXCLUSIVE);
归还某人;
}
}
}
如果希望对话框全屏显示,则根视图应为框架布局(而不是线性布局)。如果希望对话框包含工具栏,还需要第二个工具栏,因为第一个工具栏已附加到TableAyout
像这样的方法应该会奏效:
活动\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Use ThemeOverlay to make the toolbar and tablayout text
white -->
<android.support.design.widget.AppBarLayout
android:id="@+id/abl_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/CustomTabStyle"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/container">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<!-- Use ThemeOverlay to make the toolbar and tablayout text
white -->
<android.support.design.widget.AppBarLayout
android:id="@+id/abl_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="@id/toolbar_layout"
app:layout_anchorGravity="bottom|right|end"
app:borderWidth="0dp"
android:layout_margin="20dp"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/fragment_toolbar"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimaryDark"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/background_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the dialog"/>
</FrameLayout>
</LinearLayout>
fragment\u dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Use ThemeOverlay to make the toolbar and tablayout text
white -->
<android.support.design.widget.AppBarLayout
android:id="@+id/abl_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/CustomTabStyle"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/container">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<!-- Use ThemeOverlay to make the toolbar and tablayout text
white -->
<android.support.design.widget.AppBarLayout
android:id="@+id/abl_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_anchor="@id/toolbar_layout"
app:layout_anchorGravity="bottom|right|end"
app:borderWidth="0dp"
android:layout_margin="20dp"
android:clickable="true"/>
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/fragment_toolbar"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="?attr/colorPrimaryDark"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/background_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the dialog"/>
</FrameLayout>
</LinearLayout>
我建议使用对话框的活动,而不是将对话框片段附加到视图中,特别是如果您计划将输入文本放入对话框中。在使用对话框片段时,我在动画和软键盘方面遇到了一些奇怪的行为。创建新活动解决了所有这些问题 我见过很多应用程序在这样的对话框中使用
活动
,但android文档似乎建议使用对话框片段
。我想我现在发现了原因。另外,使用FrameLayout
作为根目录是否意味着工具栏+选项卡会隐藏,就像使用android.R.id.content
时一样?在这种情况下,主活动中已经有许多视图,并且需要第二个工具栏,我认为创建第二个活动是有意义的。每种情况都不同,对于更简单的视图层次结构,片段更有意义。CoordinatorLayout处理工具栏+选项卡的放置,因此不需要线性布局。如果您不再使用对话框片段,CoordinatorLayout可能是您的根视图。我对根FrameLayout
的看法是正确的,结果是相同的,并且与android.R.id.content
相同。无论如何,我认为你是对的,一个对话框片段
不足以完全覆盖所有内容