Android's Vertical Linear Layout weighting doesn't take into account actionbar height -
for spanish wotd app have 3 cardviews sitting in linear layout. top , bottom ones equally weighted , middle 1 more important weighted accordingly. no matter what, bottom 1 seems hang on screen (about size of actionbar), after changing around weights.
it appears when android calculating percentages, doesn't account actionbar @ all. i've done other people has said including changing match_parent
, 0dp height, nothing working. have no idea why.
<linearlayout 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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.cinnamint.cotidiano.mainactivity$placeholderfragment" android:orientation="vertical"> <android.support.v7.widget.cardview xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view_header" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" card_view:cardcornerradius="4dp"> <textview android:id="@+id/day_of_week_label" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.v7.widget.cardview> <android.support.v7.widget.cardview xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view_content" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" card_view:cardcornerradius="4dp"> <textview android:id="@+id/word" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <view android:layout_width="match_parent" android:layout_height="2dp" android:background="@android:color/darker_gray" /> <textview android:id="@+id/definition" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.v7.widget.cardview> <android.support.v7.widget.cardview xmlns:card_view="http://schemas.android.com/apk/res-auto" android:id="@+id/card_view_date" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" card_view:cardcornerradius="4dp"> <textview android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.v7.widget.cardview> </linearlayout>
look @ screenshot (sorry if hard see), bottom card hangs over
edit: manifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cinnamint.cotidiano"> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity" android:label="@string/app_name" android:theme="@style/apptheme.noactionbar"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
styles:
<resources> <!-- base application theme. --> <style name="apptheme" parent="theme.appcompat.light.darkactionbar"> <!-- customize theme here. --> <item name="colorprimary">@color/colorprimary</item> <item name="colorprimarydark">@color/colorprimarydark</item> <item name="coloraccent">@color/coloraccent</item> </style> <style name="apptheme.noactionbar"> <item name="windowactionbar">false</item> <item name="windownotitle">true</item> </style> <style name="apptheme.appbaroverlay" parent="themeoverlay.appcompat.dark.actionbar" /> <style name="apptheme.popupoverlay" parent="themeoverlay.appcompat.light" /> </resources>
edit: activity_main.xml
ding="utf-8"?> <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.cinnamint.cotidiano.mainactivity" app:layout_behavior="@string/appbar_scrolling_view_behavior" > <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.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:fitssystemwindows="true"/> <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:visibility="invisible" /> </android.support.design.widget.coordinatorlayout>
mainactivity
package com.cinnamint.cotidiano; import android.support.design.widget.floatingactionbutton; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar; 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.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.textview; import java.text.simpledateformat; import java.util.calendar; import java.util.date; import java.util.list; import java.util.locale; public class mainactivity extends appcompatactivity { /** * {@link android.support.v4.view.pageradapter} provide * fragments each of sections. use * {@link fragmentpageradapter} derivative, keep every * loaded fragment in memory. if becomes memory intensive, * may best switch * {@link android.support.v4.app.fragmentstatepageradapter}. */ private sectionspageradapter msectionspageradapter; private static list<words> availablewords; private long milliseconds_per_day = 24 * 60 * 60 * 1000; private long dayssincecinnamintepoch; private calendar cinnamintepoch; private static final int cinnamint_epoch_day = 15; private static final int cinnamint_epoch_month = 7 - 1; private static final int cinnamint_epoch_year = 2016; private static final string tag = "cinnamint_cotidiano"; /** * {@link viewpager} host 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); // july 14, 2016: day 1 of development // add appropriate number of days cinnamintepoch = calendar.getinstance(); cinnamintepoch.set(calendar.day_of_month, cinnamint_epoch_day); cinnamintepoch.set(calendar.month, cinnamint_epoch_month); cinnamintepoch.set(calendar.year, cinnamint_epoch_year); calendar today = calendar.getinstance(); dayssincecinnamintepoch = (long) math.ceil( (today.gettimeinmillis() - cinnamintepoch.gettimeinmillis()) / milliseconds_per_day ); log.d(tag, long.tostring(dayssincecinnamintepoch)); worddatabase wdb = new worddatabase(getapplicationcontext()); wdb.open(); availablewords = wdb.geteverywordbydate(dayssincecinnamintepoch); wdb.close(); // create adapter return fragment each of 3 // primary sections of activity. msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager(), availablewords.size()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.container); mviewpager.setadapter(msectionspageradapter); // move farthest right position mviewpager.setcurrentitem(availablewords.size() - 1); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { /** * fragment argument representing section number * fragment. */ private static final string arg_section_number = "section_number"; public placeholderfragment() { } /** * returns new instance of fragment 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 dayofweeklabel = (textview) rootview.findviewbyid(r.id.day_of_week_label); textview wordlabel = (textview) rootview.findviewbyid(r.id.word); textview definitionlabel = (textview) rootview.findviewbyid(r.id.definition); textview datelabel = (textview) rootview.findviewbyid(r.id.date); int sectionnumber = getarguments().getint(arg_section_number); // july 14, 2016: day 1 of development // add appropriate number of days calendar fragmentdate = calendar.getinstance(); fragmentdate.set(calendar.day_of_month, cinnamint_epoch_day + (sectionnumber - 1)); fragmentdate.set(calendar.month, cinnamint_epoch_month); fragmentdate.set(calendar.year, cinnamint_epoch_year); // format day , date date date = fragmentdate.gettime(); simpledateformat format = new simpledateformat("d mmmm, yyyy", new locale("es", "es")); words targetword = mainactivity.availablewords.get(sectionnumber - 1); string dayofweekcontent = (new simpledateformat("eeee", new locale("es", "es"))).format(date.gettime()); string wordcontent = targetword.gettext(); string definitioncontent = targetword.getdefinition(); string datecontent = format.format(date); dayofweeklabel.settext(dayofweekcontent); wordlabel.settext(wordcontent); definitionlabel.settext(definitioncontent); datelabel.settext(datecontent); return rootview; } } /** * {@link fragmentpageradapter} returns fragment corresponding * 1 of sections/tabs/pages. */ public class sectionspageradapter extends fragmentpageradapter { private int items = 1; public sectionspageradapter(fragmentmanager fm, int item_count) { super(fm); items = item_count; } @override public fragment getitem(int position) { // getitem called instantiate fragment given page. // return placeholderfragment (defined static inner class below). return placeholderfragment.newinstance(position + 1); } @override public int getcount() { return items; } @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; } } }
i have found fix multiple applications don't take account actionbar size adding app:layout_behavior="@string/appbar_scrolling_view_behavior"
layout. in case, put in linearlayout. also, may have put android:fitssystemwindows="true"
in linearlayout.
fragments others having issue using fragments, put line in layout (probably framelayout) contains fragment's <include/>
statement.
Comments
Post a Comment