Android的垂直线性布局加权不考虑操作栏高度

时间:2016-07-15 19:50:47

标签: android android-linearlayout

对于我的西班牙语WotD应用程序,我有3张卡片视图以线性布局。顶部和底部的权重相等,中间的一个更重要,因此它相应地加权。无论怎样,底部似乎都悬挂在屏幕上(大约是动作栏的大小),即使在改变重量之后也是如此。

似乎当Android计算百分比时,它根本不考虑ActionBar。我已经完成了其他人所说的内容,包括更改为match_parent,0dp的高度,但没有任何工作。我不明白为什么。

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

enter image description here

查看屏幕截图(对不起,如果很难看到),底部卡片明显挂起

编辑:清单

<?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>

样式:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your 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>

编辑: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 {

    /**
     * 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;

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


    /**
     * 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);

        // July 14, 2016: day 1 of development
        // Add the 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 the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), availableWords.size());

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

        // Move to 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 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 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 the 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 the day and 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;
        }
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the 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 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() {
            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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我发现多个应用程序的修复程序没有考虑到操作栏大小正在向布局添加app:layout_behavior="@string/appbar_scrolling_view_behavior"。在你的情况下,我会把它放在LinearLayout中。此外,您可能必须将android:fitsSystemWindows="true"放入LinearLayout。

<强>片段 对于使用Fragments解决此问题的其他人,请将此行放在包含Fragment <include/>语句的布局(可能是FrameLayout)中。