【问题标题】:How to add button and a processing layout on android studio如何在android studio上添加按钮和处理布局
【发布时间】:2017-12-01 18:36:10
【问题描述】:

我是 2 年电气和计算机科学专业的学生:我不熟悉 android studio(我大部分时间都在使用 C++)。

我为一个android项目制作了一个处理操纵杆,然后我将我的处理草图导出到了android studio,现在我必须找到一种方法来将我的处理草图中的数据(矢量位置)恢复到我的主要活动中。

但问题出在这里:

  • 我想在我的主要活动中添加一个 TextView,但处理草图占用了所有窗口,我无法缩小它。

所以我尝试了什么:

  • 我尝试将片段放置在相对布局中,然后放置 TextView 但它不起作用,即使我将片段制作得非常小它仍然占据整个屏幕。

我的想法是这条线有问题->

setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                     ViewGroup.LayoutParams.MATCH_PARENT));

我做了一些截图:

这里是主要活动:

 @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout frame = new FrameLayout(this);
    frame.setId(CompatUtils.getUniqueViewId());
    setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                     ViewGroup.LayoutParams.MATCH_PARENT));
   // setContentView(R.layout.main); // attach the view for this activity

    sketch = new Joystick();

    PFragment fragment = new PFragment(sketch);
    fragment.setView(frame, this);
  }

这里是主要活动的视图

【问题讨论】:

标签: android android-studio processing


【解决方案1】:

为了在您的 Main Activity 中有一个片段,您可以执行以下操作:

 public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate (Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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

            TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(mViewPager);
        }

        /**
         * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
         * one of the sections/tabs/pages.
         */
         private class YourFragmentAdapter extends FragmentPagerAdapter {

              SectionsPagerAdapter(FragmentManager fm) {
              super(fm);
         }

         @Override
         public Fragment getItem(int position) {
             switch (position) {
                 case 0:
                     return new YourFragment()
                 default:
                     return null;
            }
        }

        @Override
        public int getCount() {
            return 1;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Your fragment name";
            }
            return null;
        }
    }

    }

main.xml:

<?xml version="1.0" encoding="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">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <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.support.design.widget.CoordinatorLayout>

你的片段类:

public class YourFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

通过这种方式,您可以拥有一个包含尽可能多的视图和尽可能多的片段的活动。请注意,如果您有多个片段,您的活动将更像是一个选项卡式活动。

【讨论】:

  • 嘿!谢谢 !我今天或明天试试这个!
猜你喜欢
  • 2021-04-12
  • 2016-09-07
  • 2017-05-31
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-06
  • 2015-10-03
相关资源
最近更新 更多