Jud*_*des 6 xml android android-layout android-fragments android-fragmentactivity
我有一个需要在活动上方显示的片段,但活动中的按钮一直显示在该片段上方。我需要该片段与活动完全重叠。
活动的xml。
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_close_black_24dp"
app:popupTheme="@style/Theme.AppCompat.NoActionBar"
app:titleTextColor="@color/toolbarTextColor" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textMultiLine"
android:lines="3"
android:maxLines="10"
android:scrollbars="vertical" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
片段
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
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.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_close_black_24dp"
app:popupTheme="@style/Theme.AppCompat.NoActionBar"
app:titleTextColor="@color/toolbarTextColor" />
<TextView
android:id="@+id/firstnameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
/>
<TextView
android:id="@+id/lastnameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chat" />
<fragment
android:id="@+id/fragment_chat"
android:name="user.InitiateChat_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
private void init() {
final FragmentManager fragmentManager = getSupportFragmentManager();
fragment = fragmentManager.findFragmentById(R.id.fragment_chat);
final FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.hide(fragment);
fragmentTransaction.commit();
}
public void showChatForm() {
showHideFragment(fragment);
}
private void showHideFragment(final Fragment fragment) {
final FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction();
fragTransaction.setCustomAnimations(R.anim.fragment_slide_from_bottom, R.anim.fragment_slide_to_bottom);
if (fragment.isHidden()) {
fragTransaction.show(fragment);
} else {
fragTransaction.hide(fragment);
}
fragTransaction.commit();
}
Run Code Online (Sandbox Code Playgroud)
初始化代码在活动创建时调用。Fragment是全局变量。
Chr*_*n D 11
您的 Button 是可见的,因为在使用 Material Design 主题时它的默认高度为 2dp。您应该为按钮上方的布局提供 2 dp 或更多 dp 的高度。在您的情况下,这将使按钮消失在片段布局后面。
android:elevation="2dp"
Run Code Online (Sandbox Code Playgroud)
尝试这个:
<fragment
android:id="@+id/fragment_chat"
android:name="user.InitiateChat_Fragment"
android:layout_width="match_parent"
android:background="?android:attr/colorBackground" //add this
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
第二种方式
在片段中创建布局并在显示片段期间隐藏其可见性
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:navigationIcon="@drawable/ic_close_black_24dp"
app:popupTheme="@style/Theme.AppCompat.NoActionBar"
app:titleTextColor="@color/toolbarTextColor" />
<LinearLayout
android:id="@+id/activity_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/firstnameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin" />
<TextView
android:id="@+id/lastnameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chat" />
</LinearLayout>
<fragment
android:id="@+id/fragment_chat"
android:name="user.InitiateChat_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
现在在您的代码中:
private void showHideFragment(final Fragment fragment) {
final FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction();
fragTransaction.setCustomAnimations(R.anim.fragment_slide_from_bottom, R.anim.fragment_slide_to_bottom);
if (fragment.isHidden()) {
fragTransaction.show(fragment); //show fragment
layout.setVisibility(View.GONE); //hide Linearlayout
} else {
fragTransaction.hide(fragment);
}
fragTransaction.commit();
}
Run Code Online (Sandbox Code Playgroud)
在哪里LinearLayout layout =(LinearLayout)findViewById(R.id.activity_button);
归档时间: |
|
查看次数: |
1479 次 |
最近记录: |