运行应用程序后,我发现四个选项卡位于屏幕顶部.如何让AD位于Tab的上方?谢谢!
当前
我希望
activity_main.xml中
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:orIEntation="vertical" > <com.Google.androID.gms.ads.AdVIEw xmlns:ads="http://schemas.androID.com/apk/res-auto" androID:ID="@+ID/adVIEw" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" ads:adSize="SMART_BANNER" androID:layout_alignParenttop="true" ads:adUnitID="@string/ad_unit_ID" /> <linearLayout androID:layout_wIDth="wrap_content" androID:layout_height="fill_parent" androID:layout_below="@+ID/adVIEw" androID:layout_above="@+ID/linearLayout1" androID:paddingtop="12dip" androID:paddingleft="7dip" androID:paddingRight="7dip" androID:orIEntation="vertical" > <androID.support.v4.vIEw.VIEwPager xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/pager" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" tools:context=".MainActivity" /> </linearLayout> <linearLayout androID:ID="@+ID/linearLayout1" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical" androID:gravity="center" androID:orIEntation="horizontal" androID:layout_alignParentBottom="true" androID:weightSum="4" > <button androID:ID="@+ID/btnClose" androID:layout_wIDth="0dip" androID:layout_height="fill_parent" androID:layout_gravity="center" androID:layout_weight="1" androID:text="Close" /> </linearLayout></relativeLayout>
fragment_main.xml
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" androID:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity$PlaceholderFragment"> <TextVIEw androID:ID="@+ID/section_label1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Hello" /> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="New button" androID:ID="@+ID/button" androID:layout_alignParentBottom="true" androID:layout_centerHorizontal="true" /></relativeLayout>
MainActivity.java
public class MainActivity extends ActionBaractivity implements Actionbar.TabListener { SectionsPagerAdapter mSectionsPagerAdapter; VIEwPager mVIEwPager; private AdVIEw adVIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); adVIEw=(AdVIEw) findVIEwByID(R.ID.adVIEw); SetAD(adVIEw); // Set up the action bar. final Actionbar actionbar = getSupportActionbar(); actionbar.setNavigationMode(Actionbar.NAVIGATION_MODE_TABS); // 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.pager); mVIEwPager.setAdapter(mSectionsPagerAdapter); // When swiPing between different sections,select the corresponding // tab. We can also use Actionbar.Tab#select() to do this if we have // a reference to the Tab. mVIEwPager.setonPagechangelistener(new VIEwPager.SimpleOnPagechangelistener() { @OverrIDe public voID onPageSelected(int position) { actionbar.setSelectednavigationItem(position); } }); // For each of the sections in the app,add a tab to the action bar. for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { // Create a tab with text corresponding to the page Title defined by // the adapter. Also specify this Activity object,which implements // the TabListener interface,as the callback (Listener) for when // this tab is selected. actionbar.addTab( actionbar.newTab() .setText(mSectionsPagerAdapter.getPageTitle(i)) .setTabListener(this)); } } @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; } public voID SetAD(AdVIEw adVIEw) { if (true) { AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMulATOR) .build(); adVIEw.loadAd(adRequest); }else{ adVIEw.setVisibility(VIEw.GONE); } } @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); } @OverrIDe public voID onTabSelected(Actionbar.Tab tab,FragmentTransaction fragmentTransaction) { // When the given tab is selected,switch to the corresponding page in // the VIEwPager. mVIEwPager.setCurrentItem(tab.getposition()); } @OverrIDe public voID onTabUnselected(Actionbar.Tab tab,FragmentTransaction fragmentTransaction) { } @OverrIDe public voID onTabReselected(Actionbar.Tab tab,FragmentTransaction fragmentTransaction) { } 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); Fragment fragment; switch(position){ case 0: fragment = new PlaceholderFragment(); break; case 1: fragment = new PlaceholderFragment1(); break; case 2: fragment = new PlaceholderFragment1(); break; case 3: fragment = new PlaceholderFragment(); break; default: throw new IllegalArgumentException("InvalID section number"); } //set args if necessary Bundle args = new Bundle(); args.putInt(PlaceholderFragment.ARG_SECTION_NUMBER,position + 1); fragment.setArguments(args); return fragment; } @OverrIDe public int getCount() { // Show 3 total pages. return 4; } @OverrIDe public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return getString(R.string.Title_section1).toupperCase(l); case 1: return getString(R.string.Title_section2).toupperCase(l); case 2: return getString(R.string.Title_section3).toupperCase(l); case 3: return getString(R.string.Title_section4).toupperCase(l); } return null; } } /** * 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_number1"; /** * 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; } public PlaceholderFragment() { } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { VIEw rootVIEw = inflater.inflate(R.layout.fragment_main,container,false); return rootVIEw; } } /** * A placeholder fragment containing a simple vIEw. */ public static class PlaceholderFragment1 extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; /** * Returns a new instance of this fragment for the given section * number. */ public static PlaceholderFragment1 newInstance(int sectionNumber) { PlaceholderFragment1 fragment = new PlaceholderFragment1(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER,sectionNumber); fragment.setArguments(args); return fragment; } public PlaceholderFragment1() { } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,Bundle savedInstanceState) { VIEw rootVIEw = inflater.inflate(R.layout.fragment_main1,false); button cw= (button)rootVIEw.findVIEwByID(R.ID.buttonAA); final LayoutInflater my=inflater; cw.setonClickListener(new VIEw.OnClickListener(){ @OverrIDe public voID onClick(VIEw v) { Toast.makeText(my.getContext(),"CW",Toast.LENGTH_SHORT).show(); } }); return rootVIEw; } }}解决方法 原因是您的布局位于Actionbaractivity内部,当您设置布局时,它将始终位于 *** 作栏中(在您的情况下是标题栏和选项卡). 要实现您想要的功能,请创建自己的自定义标题栏和标签. 总结
以上是内存溢出为你收集整理的为什么AD不能位于Android Tabbed活动的屏幕顶部?全部内容,希望文章能够帮你解决为什么AD不能位于Android Tabbed活动的屏幕顶部?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)