Android 在活动'上添加片段覆盖;s观点
我是安卓系统的新手 下面是我所拥有的和我真正需要的 我有一个活动,其中我声明了一个自定义视图,其中包含一组按钮,这些按钮单击事件显示各自的片段 此自定义视图将显示在每个片段的顶部 现在假设在自定义视图的第一个按钮上,我是一个显示列表视图的显示片段。。 点击列表视图,它会显示另一个片段,即细节片段 详细的片段。。我有一个按钮,我需要显示一个主要活动的片段覆盖。。。就像在全屏上一样。。我怎样才能做到这一点Android 在活动'上添加片段覆盖;s观点,android,android-fragments,Android,Android Fragments,我是安卓系统的新手 下面是我所拥有的和我真正需要的 我有一个活动,其中我声明了一个自定义视图,其中包含一组按钮,这些按钮单击事件显示各自的片段 此自定义视图将显示在每个片段的顶部 现在假设在自定义视图的第一个按钮上,我是一个显示列表视图的显示片段。。 点击列表视图,它会显示另一个片段,即细节片段 详细的片段。。我有一个按钮,我需要显示一个主要活动的片段覆盖。。。就像在全屏上一样。。我怎样才能做到这一点 在我看来,导致这种行为的最简单方法是使用活动覆盖或DialogFragment。例如: 活动覆
在我看来,导致这种行为的最简单方法是使用活动覆盖或DialogFragment。例如: 活动覆盖
MainActivity.java MainActivity.xml SecondActivity.xml MainActivity.java 其他文件
其他文件与上面的示例相同。如果我遵循您的问题,您应该在主活动的布局中使用RelativeLayout作为覆盖。并根据需要设置可见性 layout\u activity\u main.xml
<RelativeLayout
android:width="match_parent"
android:height="match_parent">
<LinearLayout>
// your toolbar
// You fragment container
// your main layout goes here
</LinearLayout>
<RelativeLayout
android:id="@+id/overlay"
android:width="match_parent"
android:height="match_parent">
</RelativeLayout>
</RelativeLayout>
//你的工具栏
//你可以使用碎片容器
//你的主要布局在这里
像这样:
<LinearLayout id = "@+id/container">
<ToolBar id ="@+id/toolbar>
</ToolBar>
<FrameLayout id ="@+id/main_containt"/>
</LinearLayout>
getSupportFragmentManager().add(new Fragment(),R.id.container).commit();
你能提供简单的绘画图片来解释你的布局吗?像这样的,提及按钮会在所有的片段中被修复。你在使用tablayout吗?您想在两个视图中访问您的表格布局?这就是您想要的吗?并将您尝试过的内容粘贴到此处?嗨。。谢谢你的努力。。真的很感激…但是亲爱的,我不想要第二个活动,因为我只是用片段做所有的事情。片段部分是固定的,正如我所说,我有一个主要的活动,其中包含一个布局和一组按钮,这将对我的所有片段可见。。。现在,我的一个片段需要显示在activty@user5457212活动布局中已经嵌套了一些片段。现在您想从布局中删除其中一个并使其全屏显示吗?或者可能创建一个新的浮动布局?@user5457212我已经使用DialogFragment添加了新的解决方案。
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
<RelativeLayout 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:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".SecondActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity"
android:textSize="30dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
</RelativeLayout>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
<style name="TransparentFloatingActivity" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arturszymanski.test" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:theme="@style/TransparentFloatingActivity"
android:name=".SecondActivity"
android:label="@string/title_activity_second" >
</activity>
</application>
</manifest>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MyDialogFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FullScreen Fragment"
android:textSize="30dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
public class MyDialogFragment extends DialogFragment
{
private int margin;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
margin = 10;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_my_dialog, container, false);
}
@Override
public void onResume() {
int dialogHeight = MainActivity.displayMetrics.heightPixels - (margin * 2) - MainActivity.StatusBarHeight;
int dialogWidth = MainActivity.displayMetrics.widthPixels - (margin * 2);
getDialog().getWindow().setLayout(dialogWidth, dialogHeight);
super.onResume();
}
}
public class MainActivity extends AppCompatActivity
{
public static DisplayMetrics displayMetrics;
public static int StatusBarHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
StatusBarHeight = getStatusBarHeight();
Button button = (Button) findViewById(R.id.buttonShow);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getFragmentManager(), "Dialog");
}
});
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
<RelativeLayout
android:width="match_parent"
android:height="match_parent">
<LinearLayout>
// your toolbar
// You fragment container
// your main layout goes here
</LinearLayout>
<RelativeLayout
android:id="@+id/overlay"
android:width="match_parent"
android:height="match_parent">
</RelativeLayout>
</RelativeLayout>
<LinearLayout id = "@+id/container">
<ToolBar id ="@+id/toolbar>
</ToolBar>
<FrameLayout id ="@+id/main_containt"/>
</LinearLayout>
getSupportFragmentManager().add(new Fragment(),R.id.container).commit();