[英]Sizing android tabs and fragments
我是Android的新手,但知道如何使用Java,因此我只需要有關Application的某些布局部分的幫助。
基本上,我希望在應用程序的頂部有4個選項卡(我已完成此操作),並且在每個選項卡上單擊時,將顯示一個不同的片段(非常新,因此可能不正確),這將達到不同的目的。
我不確定如何解決的第一個問題是,我希望前三個選項卡(鍵盤,媒體遙控器和鼠標)大於設置選項卡(如果可能的話,將更改為圖標)。 我希望前三個的寬度相等,而第四個的寬度要小得多。
其次,當單擊相應的選項卡時,我將如何出現不同的片段(例如,單擊“ MEDIA REMOTE”將打開mediaRemoteFragment)。 該應用程序將是一個客戶端,因此需要一個主體部分(MainActivity.java ??)在后台運行,然后這些片段會將消息發送到主體,然后再將這些消息發送到服務器。
抱歉,這還不清楚...
這是我當前的代碼:
MainActivity.java:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import layout.FragmentTab;
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("keyboardTab").setIndicator("Keyboard", null), FragmentTab.class, null);
mTabHost.addTab(mTabHost.newTabSpec("mediaRemoteTab").setIndicator("Media Remote", null), FragmentTab.class, null);
mTabHost.addTab(mTabHost.newTabSpec("mouseTab").setIndicator("Mouse", null), FragmentTab.class, null);
mTabHost.addTab(mTabHost.newTabSpec("settingsTab").setIndicator("Settings", null), FragmentTab.class, null);
}
}
FragmentTab.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import kaihulme.tabs.R;
public class FragmentTab extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_layout, container, false);
TextView tv = (TextView) v.findViewById(R.id.text);
tv.setText(this.getTag() + " Content");
return v;
}
}
activity_main.xml:
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
fragment_layout.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#eaecee">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="hello_world"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
我看過您的布局,它們對我來說是嚴格的。 首先,您必須創建4個片段。 您將需要在選項卡上實現onClik()
偵聽器以遵循此過程:單擊選項卡時,實際的fragment
應消失,因此應將其刪除,並應創建一個新的片段並將其插入到FrameLayout
。 為此,可以使用方法replace()
。
android studio自動生成的片段模板非常有用。 在這里,您有很多可以處理片段的信息: http : //developer.android.com/intl/es/guide/components/fragments.html
最重要的是:當您需要替換片段時,請刪除舊的片段,並使用android studio提供的newInstance()
方法創建新的片段,您必須根據需要更改此方法。
KeyboardFragment mFrag = KeyboardFragment.newInstace(arg1, arg2);
您可以調用片段方法來保存創建的片段的實例:
mFrag.myMethod();
但是您不能從片段中調用Activity方法。 要共享信息,注意某些事件等,您必須使用上一文章中所述的接口:
public static class FragmentA extends ListFragment {
OnArticleSelectedListener mListener;
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnArticleSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
}
}
...
}
活動必須實現該接口,然后您可以調用接口方法以將片段與容器活動進行通信。
希望它能幫助您開始工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.