![](/img/trans.png)
[英]How to go to another activity in Fragments | Android Studio
[英]How can I get a fragment to go under 2 other fragments? (Android Studio)
我正在嘗試讓一個片段進入其他2個片段的下方,同時讓一個片段在屏幕的每一側延伸到整個屏幕。
我正在使用一個線性布局,我認為這是有問題的,但是,我也嘗試使用相對布局和框架布局,但無法使它們按照我想要的方式排列。
MainActivity.Java
package fragmentexample.example.com.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Fragment1.java(所有5個片段都相同)
package fragmentexample.example.com.myapplication;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1,container, false);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/fragment1"
android:name="fragmentexample.example.com.myapplication.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_gravity="top"
android:layout_weight="1"
/>
<fragment
android:id="@+id/fragment2"
android:name="fragmentexample.example.com.myapplication.Fragment2"
android:layout_width="0px"
android:layout_height="273dp"
android:layout_gravity="top"
android:layout_weight="1"
/>
<fragment
android:id="@+id/fragment5"
android:name="fragmentexample.example.com.myapplication.Fragment5"
android:layout_width="0px"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:layout_weight="1"
/>
<fragment
android:id="@+id/fragment3"
android:name="fragmentexample.example.com.myapplication.Fragment3"
android:layout_width="0px"
android:layout_height="273dp"
android:layout_gravity="top"
android:layout_weight="1"
/>
<fragment
android:id="@+id/fragment4"
android:name="fragmentexample.example.com.myapplication.Fragment4"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_gravity="top"
android:layout_weight="1"
/>
fragment1.xml(對於所有5個片段都相同)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF0000"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
更簡單的方法是嵌套LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<fragment
android:id="@+id/fragment_red"
android:name="fragmentexample.example.com.myapplication.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_gravity="top"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<fragment
android:id="@+id/fragment_blue"
android:name="fragmentexample.example.com.myapplication.Fragment2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
<fragment
android:id="@+id/fragment_yellow"
android:name="fragmentexample.example.com.myapplication.Fragment5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
/>
</LinearLayout>
<fragment
android:id="@+id/fragment_green"
android:name="fragmentexample.example.com.myapplication.Fragment3"
android:layout_height="0dp"
android:layout_width="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
<fragment
android:id="@+id/fragment_puple"
android:name="fragmentexample.example.com.myapplication.Fragment4"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_gravity="top"
android:layout_weight="1"
/>
</LinearLayout>
雖然嵌套的LinearLayouts不利於性能,但具有硬編碼的尺寸也很糟糕。
嘗試利用該layout_weight
屬性來“填補空白”並擴展視圖。
注意:如果需要,可以使用Fragment標簽替換FrameLayouts,但是您可以通過編程方式用Fragment填充FrameLayout。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4">
<FrameLayout
android:layout_width="0dp"
android:layout_weight="1"
android:background="@android:color/holo_red_dark"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="2"
android:orientation="vertical"
android:weightSum="2"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:orientation="horizontal">
<FrameLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_blue_light"
android:layout_height="match_parent">
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:background="#ffea00"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_green_light"
android:layout_height="0dp">
</FrameLayout>
</LinearLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_weight="1"
android:background="@android:color/holo_purple"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.