如何在android studio上添加按钮和处理布局
我是一名2年制电气和计算机科学专业的学生:我不熟悉android studio(大部分时间我在C++上工作) 我为一个android项目制作了一个处理操纵杆,然后我将我的处理草图导出到android studio,现在我必须找到一种方法将数据(向量位置)从我的处理草图恢复到我的主要活动 但问题是:如何在android studio上添加按钮和处理布局,android,android-studio,processing,Android,Android Studio,Processing,我是一名2年制电气和计算机科学专业的学生:我不熟悉android studio(大部分时间我在C++上工作) 我为一个android项目制作了一个处理操纵杆,然后我将我的处理草图导出到android studio,现在我必须找到一种方法将数据(向量位置)从我的处理草图恢复到我的主要活动 但问题是: 我想在我的主要活动中添加一个TextView,但是处理草图占据了整个窗口,我无法减少它 那么我尝试了什么: 我试着将片段放在一个相对的布局中,然后放置TextView,但它没有工作,即使我将片段
- 我想在我的主要活动中添加一个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);
}
这里是主要活动的视图
为了在您的主要活动中包含一个片段,您可以执行以下操作:
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 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>
通过这种方式,您可以拥有一个包含任意多个视图和任意多个片段的活动。请注意,如果您有多个片段,您的活动将更像一个选项卡式活动。请不要发布代码的屏幕截图。请在您的问题中直接发布一个。为什么不使用xml?请不要发布代码的截图。请在你的问题中直接发表一篇文章。你为什么不使用xml?嘿!谢谢我今天或明天会试试这个!嘿谢谢我今天或明天会试试这个!