4

我只是在Android Studio 1.5.1上創建了TabbedActivity。我沒有更改任何代碼,但Fragment不適合屏幕作爲高度和ActionBar是可移動的。在Android的默認選項卡式活動,片段不適合屏幕作爲高度和actionBar可移動

Views在底部是不可見的。我需要移動ActionBar最多見底部的觀點(見視頻請:Video)。

所以,我有2個問題:

  1. 我把ListViewFragment後,我怎麼能移動ActionBar起來順手當用戶滾動ListView? (WhatsApp的和Viber的是使用這種技術)

2.How我可以防止ActionBar移動和對準Fragment到屏幕的底部?

下面是Android的創建Main Activity

public class MainActivity extends AppCompatActivity { 

/** 
* The {@link android.support.v4.view.PagerAdapter} that will provide 
* fragments for each of the sections. We use a 
* {@link FragmentPagerAdapter} derivative, which will keep every 
* loaded fragment in memory. If this becomes too memory intensive, it 
* may be best to switch to a 
* {@link android.support.v4.app.FragmentStatePagerAdapter}. 
*/ 
private SectionsPagerAdapter mSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will host the section contents. 
*/ 
private ViewPager mViewPager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(mViewPager); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    private static final String ARG_SECTION_NUMBER = "section_number"; 

    public PlaceholderFragment() { 
    } 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     TextView textView = (TextView) rootView.findViewById(R.id.section_label); 
     textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); 
     return rootView; 
    } 
} 

/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
* one of the sections/tabs/pages. 
*/ 
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a PlaceholderFragment (defined as a static inner class below). 
     return PlaceholderFragment.newInstance(position + 1); 
    } 

    @Override 
    public int getCount() { 
     // Show 3 total pages. 
     return 3; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: 
       return "SECTION 1"; 
      case 1: 
       return "SECTION 2"; 
      case 2: 
       return "SECTION 3"; 
     } 
     return null; 
     } 
    } 
} 

activity_main.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:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.eren.siiilllfdjfds.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/appbar_padding_top" 
     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:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

     </android.support.v7.widget.Toolbar> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

編輯:enter image description here

我想我需要改變這一行容器:

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

編輯2:當我刪除我在1中提到的代碼編輯,片段對齊底部,但片段開始對齊屏幕頂部,我的意思是片段落後actionBar

+0

可能重複的[調整視圖大小,viewpager,彌補選項卡高度](http://stackoverflow.com/questions/33944100/adjust-view-size-inside-viewpager-make-up-for-tab -height) – tbm

回答

2

我把片段ListView後,我怎麼能移動ActionBar起來順手 當用戶滾動的ListView? (的WhatsApp和Viber的使用此 技術)

首先,更好地/或讓我們說你必須使用RecyclerView有:

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

如這裏提到的:(由在以下)

https://github.com/codepath/android_guides/wiki/Handling-Scrolls-with-CoordinatorLayout#responding-to-scroll-events

以及有關像WhatsApp設計中,我使用以下代碼:

<android.support.design.widget.CoordinatorLayout 
     android:id="@+id/root_coordinator" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tab_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="?attr/colorPrimary" 
       android:minHeight="?attr/actionBarSize" 
       app:tabIndicatorColor="#ffffff" 
       app:tabIndicatorHeight="4dp" 
       app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

      <android.support.v4.view.ViewPager 
       android:id="@+id/pager" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 

     </LinearLayout> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:elevation="0dp"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:layout_scrollFlags="scroll|enterAlways" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
       app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

     </android.support.design.widget.AppBarLayout> 

     <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_send_white_18dp" 
      app:backgroundTint="@color/colorAccent" /> 

    </android.support.design.widget.CoordinatorLayout> 

當我刪除我在1編輯提到的代碼,片段對準 底部,但片段的開始對齊到屏幕的頂部,我的意思是片段 落後於actionBar。

您必須添加:

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

對於使用內容AppBarLayout和固定一切之下。

https://github.com/codepath/android_guides/wiki/Handling-Scrolls-with-CoordinatorLayout#responding-to-scroll-events

它說:

注: AppBarLayout目前預計是根據谷歌官方文件嵌套在一個CoordinatorLayout內的第一個孩子。

接下來,我們需要定義將滾動的AppBarLayout和 ,View之間的關聯。app:layout_behavior添加到 R ecyclerView或任何其他View能夠nested scrollingNestedScrollView。支持庫包含映射到 AppBarLayout.ScrollingViewBehavior的特殊字符串 resource @string/appbar_scrolling_view_behavior,該字符串用於在此特定視圖上發生滾動事件時通知 AppBarLayout。必須在觸發事件的視圖上建立 行爲。

編輯:如果你需要固定該Toolbar那裏,只需使用:

app:layout_collapseMode="pin" 

最後:

 <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_collapseMode="pin"> 
+0

謝謝你的回答!我會試試這個。但是我的問題呢? – eren130

+0

我們在StackOverFlow中使用了一個線程的問題btw,嘗試刪除它並看到:'android:paddingTop =「@ dimen/appbar_padding_top」'答案是你必須使用'app:layout_behavior',如果它沒有'工作(它應該btw)工作,告訴我自己嘗試一下。 – Mohsen

+0

'@ dimen/appbar_padding_top'爲8dp。這還不夠,我需要'100dp'來抵消片段的頂部,這不是一個好的做法,因爲'actionBar'仍然是可移動的。我仍然無法找到修復'actionBar'和修剪工作區的方法。 順便說一句,非常感謝您的關注和詳細的答案。在我嘗試之後,我會接受你的回答,首先我需要爲2.問題找到答案。 – eren130

7

我運行Android 2,所以我不知道這是否適用於1.5。

裏面主要活動XML,如由Android被自動生成有工具欄小工具:

<android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

當我刪除這一行:

 app:layout_scrollFlags="scroll|enterAlways" 

...走了所有的問題。

,所以你能看到什麼裏面了要顯示在所有視圖(片段或活動)

因此,我將集中力量查處該行屏幕不會再滾動屏幕高度正確計算:

app:layout_scrollFlags="scroll|enterAlways" 
+1

這個答案適合我!簡單而有效。這是一個真正的恥辱,這行XML標記是由Android Studio嚮導自動添加的,並導致我稱之爲「意外行爲」。謝謝你的回答。這是一個很大的幫助。 – Barns

+0

這也適用於我! – Annihil

相關問題