2015-06-12 19 views
7

Sekmeli bir etkinlik oluşturuyorum ve android: AD: ad = android: id = "@ + id/adView" öğesini android kullanılarak ekranın üst tarafına yerleştirdim: layout_alignParentTop = "true".Neden Android Sekmeli etkinlikte ekranın üst kısmında AD bulunamıyor?

Uygulamayı çalıştırdıktan sonra, dört sekmenin ekranın üst kısmında bulunduğunu buldum. AD'yi Sekmenin yukarısında nasıl yapabilirim? Teşekkürler!

Güncel umarım

enter image description here

enter image description here

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, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main1, container, 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; 
     } 
    } 

} 
+0

için gerçekten Araç Çubukları kavramını şimdilik olarak komple kod verir ancak bunu yaparsanız bilemez, bu Farklı bir düzende oluşturulduğunda araç çubuğuna neden olmanıza yardımcı olabilir, etiketini kullanarak ana düzenlerinizin herhangi birinde kullanılabilir. Şimdi, bu araç çubuğu istediğiniz ekranda herhangi bir yere yerleştirilebilir, böylece yapmayı planladığınız şeyi başarmanıza yardımcı olabilir. – mike20132013

cevap

1

nedeni, düzen bir ActionbarActivity içinde olduğunu ve size düzenini ayarlamak zaman eylem içinde hep seveceğim ki çubuğu (sizin için başlık çubuğu ve sekmeler). İstediğinizi elde etmek için kendi özel başlık çubuğunuzu ve sekmelerinizi oluşturun.

+0

Teşekkürler! Özel başlık çubuğu ve sekmeler oluşturma konusunda bana bir örnek verebilir misiniz? – HelloCW

1

Çözüm-1: Ekranın üst kısmındaki TabBar hizalamak ve ActionBar kurtulmak istiyorsanız

, bu deneyebilirsiniz:

<style name="MyTheme" parent="android:Theme.Holo.Light"> 
    <item name="android:windowActionBar">false</item> 
    <item name="android:windowNoTitle">true</item> 
</style> 

sizin styles.xml bu öğeleri hem Ekle Eylem çubuğundan eylem çubuğunu kaldırmak için res/values klasörünün altında.

Ayrıca aşağıda gibi manifest bu aktivite için tema ayarlamak zorunda:

<activity 
     android:name=".MainActivity" 
     android:theme="@style/MyTheme" /> 

Çözüm-2:

bu şekilde kolayca düzende sekmeleri konumlandırabilirsiniz, ToolBar kullanmayı düşünün. here is link to use Toolbar

+0

Teşekkürler! parent = "android: Theme.Holo.Light" API 11 gerektirir, ancak min API'm 9 – HelloCW

+0

@ HelloCW: Bu durumda, minSdk'yi destekleyen temayı kullanmayı düşünün. – astuter

1

ActionBar ürününüzün herhangi bir menü maddesi bulunmamaktadır. Yani ActionBar'u gizlemek ve reklamı Tab'un üstüne yerleştirmek mümkündür.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     ads:adSize="SMART_BANNER" 
     android:layout_alignParentTop="true" 
     ads:adUnitId="@string/ad_unit_id" /> 

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

</RelativeLayout> 

Toolbar kullanmayı düşünün, ve daha iyi bir yol bulmuş olacak: Bu gibi görünmelidir

... 
setContentView(R.layout.activity_main); 
getSupportActionBar().hide(); 

Ve activity_main için düzen: En MainActivity olarak, diyoruz.Eğer Toolbar kullanıyorsanız eğer, senin düzen bu gibi görünmelidir:

oluşturabilir ve sekmeler için bir düzen yönetmek zorunda nedenle, gerektiğinde yerleştirmek özgürsünüz
<RelativeLayout 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.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     android:layout_alignParentTop="true" /> 

    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:adSize="SMART_BANNER" 
     app:adUnitId="@string/ad_unit_id" 
     android:layout_below="@+id/toolbar" /> 

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

</RelativeLayout> 
+0

Teşekkürler! ancak yolunuzu kullanmaya çalıştığımda Sekmeler (SECTION1, SECTION2, SECTION3, SECTION4) gizlenir. – HelloCW

1

: Eğer İşlem Çubuğu en sekmeleri kullanırsanız, AB'nin altına yerleştirilecekler. Örneğin bu solution'a bakın. Sdk örnekleri klasörünün altında olduğuna inanıyorum.

1

Özel sekme düzenine ihtiyacınız var. Örneğin, kullanıma hazır ViewPagerIndicator çözümünü kullanabilirsiniz. Sizin ana düzen böyle olacak:

<RelativeLayout 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"> 

    <com.google.android.gms.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:adSize="SMART_BANNER" 
     app:adUnitId="@string/ad_unit_id" 
     android:layout_alignParentTop="true" /> 

    <com.viewpagerindicator.TabPageIndicator 
     android:id="@+id/page_indicator" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/adView" /> 

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

</RelativeLayout> 

Bind göstergesi ViewPager

//Set the pager with an adapter 
ViewPager pager = (ViewPager)findViewById(R.id.pager); 
pager.setAdapter(new TestAdapter(getSupportFragmentManager())); 

//Bind the title indicator to the adapter 
TabPageIndicator tabIndicator = (TabPageIndicator)findViewById(R.id.page_indicator); 
tabIndicator.setViewPager(pager); 
İlgili konular