如何在半屏幕上显示活动?

时间:2014-06-16 14:16:58

标签: android xml

我想制作一个出现在半屏上的活动(就像对话框但不是对话框)我想在其上添加一些文字,切换按钮和标签?

我试过Dialog Boxes,但它不满足我所以现在我想调整活动的大小。 帮助

提前致谢

2 个答案:

答案 0 :(得分:3)

易。只是使用片段!使用片段,您可以完全控制所有内容的大小和位置。看我的例子

这是我的主要xml。这是两个独立的片段。每个占据屏幕的四分之一。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:padding="12dp">

<fragment
    android:name="com.diamond.home.fragments.TopFragment"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

 <!-----here, add the rest of your xml for your main activity -------->

显示这些片段的主要活动不需要做任何事情来显示片段,因为您的XML已经为您调用了片段。注意:如果只想在特定时间显示片段,可以从xml引用fragment,并将可见性设置为GONE。然后,当您需要它时,将其设置为VISIBLE。

然后,创建像这样的片段类:

 public class TopFragment extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragments_top, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

   //do your stuff for your fragment here


}

因此,您还需要为顶部片段创建另一个xml布局。在那里,你可以添加按钮,文本视图或你有什么。希望这可以帮助。如果您有任何问题随时问。

答案 1 :(得分:2)

使用Theme.Holo.Dialog或从中继承的内容作为您的活动主题(例如,android:theme="@android:style/Theme.Holo.Dialog"元素上的<activity>

相关问题