【发布时间】:2016-07-05 17:43:21
【问题描述】:
我想打开一个 Fragment 并想放大该 Fragment 所在的视图。这可能吗? 我搜索了这些问题:
- error inflating class fragment fragment did not create a view;
- calling fragment from activity;
- how to open specific fragment from other activity onclick;
- open fragment from activity;
- how do i prevent overlapping in android;
我找不到我的答案,或者我忽略了它。是否有可能在我的布局按下我的片段下方的按钮(beetInfosButton)(封装在滚动视图中)之后打开我的片段时,我的片段不会重叠?我是否必须使用其他布局而不是RelativeLayout?或者这是不可能的。希望有人能理解我想要什么。提前感谢
这是活动代码。
public class InfoSeite extends AppCompatActivity implements BodenSeite.OnFragmentInteractionListener {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_seite);
public void buttonBodenInfos(View view){
getFragmentManager().beginTransaction().add(R.id.fragment_container,new BodenSeite()).commit();
}
还有 Activity XML 文件
<?xml version="1.0" encoding="utf-8"?> <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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#2fb215"
android:id="@+id/infoSeite">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bodenInfosString"
android:id="@+id/bodenInfosButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp"
android:onClick="buttonBodenInfos"/>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView2"
android:layout_toEndOf="@+id/bodenInfosButton"
android:layout_below="@+id/bodenInfosButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" >
<FrameLayout
android:id="@+id/fragment_container"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
</ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/beetInfosString"
android:id="@+id/beetInfosButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="buttonBeetInfos" />
</RelativeLayout>
以及片段 XML 的示例。
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#2fb215"
android:columnOrderPreserved="true"
android:tag="BodenFragment"
android:id="@+id/bodenFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bodenArtenString"
android:id="@+id/bodenSeiteUeberschrift"
android:layout_row="0"
android:layout_column="0"
android:textSize="40dp"
android:textAlignment="center" />
【问题讨论】:
-
如果我正确理解了您的问题:由于您的
ScrollView被定义为低于您的Button,因此它将始终位于ScrollView的顶部(因此您的FrameLayout)。您可以更改您的activity_main.xml,使您的Button将“消失”,或者您可以自己处理Button的Visibility,因此在buttonBodenInfos方法中,调用view.setVisibility(View.GONE)。每当您关闭Fragment时,您都需要将其设置为View.VISIBLE。 -
谢谢。但是就没有别的办法了吗?通过将按钮移到下方或其他什么?我必须让它们不可见吗?如果没有其他可能,我会试试这个。
-
我没有正确阅读您的问题。 ^^'我会添加一个答案。
标签: java android android-layout android-fragments