LinearLayout在底部推出Button

时间:2017-08-30 10:04:08

标签: java android button

我有一个TabActivity,其中有一个XML选项卡。在此选项卡中,XML文件中有一个LinearLayout和一个Button(以及其他一些东西)。我希望Button(button1)出现在设备的底部,LinearLayout出现在Button的上方。
在预览中,它可以正常工作。但是当我在Android模拟器中实际运行它时,Button会消失,只会显示LinearLayout。

主要活动文件:

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);
    }

    @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 = null;
            /*TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));*/
            if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
                rootView = inflater.inflate(R.layout.tab_1, container, false);
                setup_tab(rootView, 1);
            } else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
                rootView = inflater.inflate(R.layout.tab_2, container, false);
                setup_tab(rootView, 2);
            } else if (getArguments().getInt(ARG_SECTION_NUMBER) == 3) {
                rootView = inflater.inflate(R.layout.tab_3, container, false);
                setup_tab(rootView, 3);
            }

            View tab_1 = inflater.inflate(R.layout.tab_1, container, false);
            LinearLayout mainContent = (LinearLayout)tab_1.findViewById(R.id.main_content);
            ViewGroup.LayoutParams mainContent_layoutParams = mainContent.getLayoutParams();
            int mainContent_height = mainContent_layoutParams.height;
            int mainContent_finalHeight = mainContent_height - 60;
            mainContent_layoutParams.height = mainContent_finalHeight;
            mainContent.setLayoutParams(mainContent_layoutParams);

            return rootView;
        }

        public int pxToDp(int px) {
            DisplayMetrics metrics = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
            float logicalDensity = metrics.density;
            float dp_float = px * (160 / logicalDensity);
            int dp = Math.round(dp_float);
            return dp;
        }

        public void setup_tab(View rootView, int tab_num) {

            switch (tab_num) {
                case 1:

                    break;
                case 2:

                    break;
                case 3:

                    break;
            }

        }
    }

    /**
     * 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 "Tab 1";
                case 1:
                    return "Tab 2";
                case 2:
                    return "Tab 3";
            }
            return null;
        }
    }
}

anxmlfile.xml:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent">

        <Button
            android:id="@+id/button1"
            android:text="Button 1"
            android:layout_width="match_parent"
            android:layout_height="57dp"
            android:layout_alignParentBottom="true" />

        <LinearLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/button1"
            android:orientation="vertical"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp">

            <RadioGroup
                android:id="@+id/radio_group_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <RadioButton
                    android:id="@+id/radio_button_1"
                    android:text="First Radio Button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <RadioButton
                    android:id="@+id/radio_button_2"
                    android:text="Second Radio Button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </RadioGroup>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ProgressBar
                    android:id="@+id/progress_bar_1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

                <TextView
                    android:text="ABCDEFG"
                    android:id="@+id/progress_txt_1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textColor="@color/black"
                    android:textSize="38dp"
                    android:textStyle="bold"
                    android:background="#00000000"
                    android:layout_alignLeft="@id/progress_bar_1"
                    android:layout_alignRight="@id/progress_bar_1"
                    android:layout_alignTop="@id/progress_bar_1"
                    android:layout_alignBottom="@id/progress_bar_1"/>

            </RelativeLayout>

        </LinearLayout>

    </RelativeLayout>

预览:

In Preview

现状:

In Actuality

5 个答案:

答案 0 :(得分:3)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp">

        <RadioGroup
            android:id="@+id/radio_group_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/radio_button_1"
                android:text="First Radio Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <RadioButton
                android:id="@+id/radio_button_2"
                android:text="Second Radio Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </RadioGroup>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ProgressBar
                android:id="@+id/progress_bar_1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <TextView
                android:text="ABCDEFG"
                android:id="@+id/progress_txt_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="@color/black"
                android:textSize="38dp"
                android:textStyle="bold"
                android:background="#00000000"
                android:layout_alignLeft="@+id/progress_bar"
                android:layout_alignRight="@+id/progress_bar"
                android:layout_alignTop="@+id/progress_bar"
                android:layout_alignBottom="@+id/progress_bar"/>

        </RelativeLayout>

    </LinearLayout>
<Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:text="Button 1" />
</FrameLayout>

答案 1 :(得分:1)

在滚动视图中创建LinearLayout,如:

    <ScrollView
        android:fillViewport="true"
        android:layout_width="match_parent"
        android:layout_above="@id/button1"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp">

    ...</LinearLayout>

    </ScrollView>

答案 2 :(得分:1)

@WhatsYourIdea试试这个,如果这不起作用我就没有​​更多的想法了:

<LinearLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp">

    <RadioGroup
        android:id="@+id/radio_group_1"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radio_button_1"
            android:text="Radio Button 1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/radio_button_2"
            android:text="Radio Button 2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RadioGroup>

    <RelativeLayout
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ProgressBar
            android:id="@+id/progress_bar_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="Some Text"
            android:id="@+id/progress_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/black"
            android:textSize="38dp"
            android:textStyle="bold"
            android:background="#00000000"
            android:layout_alignLeft="@id/progress_bar_1"
            android:layout_alignRight="@id/progress_bar_1"
            android:layout_alignTop="@id/progress_bar_1"
            android:layout_alignBottom="@id/progress_bar_1"/>

    </RelativeLayout>


    <Button
    android:layout_weight="1"
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button 1" />

</LinearLayout>

然后,为每个元素调整android:weight以使其适合。

答案 3 :(得分:0)

如果您不想滚动,请使用相对布局而不是线性布局。在这种情况下,相对布局可以帮助您使用线性布局的权重

答案 4 :(得分:0)

您的进度条高度需要包装内容。使用match_parent填充整个重新布局,不会留下任何按钮空间。用ScrollView包装你的relativeLayout。