如何在活动中制作片段布局?

时间:2016-04-29 05:13:17

标签: android android-layout android-fragments

如何制作活动和片段布局?

我有主要活动布局,包含4个片段(它们交错排列)。 我写了两个。它们很相似。

<?xml version="1.0" encoding="utf-8"?>

<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="net.user.app.MainActivity"
android:layout_row="1"
android:layout_column="6"
android:layout_weight="10"
>

<GridLayout
    android:layout_width="250dp"
    android:layout_height="118dp"
    android:layout_row="1"
    android:layout_column="0">

    <fragment
        android:name="net.company.app.MyFragment"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/fragment_1"

        android:layout_row="1"
        android:layout_column="0"
        android:layout_marginEnd="5dp"
        android:layout_marginTop="5dp"
        tools:layout="@layout/fragment_layout" /> 

 <fragment
        android:name="net.company.app.MyFragment"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/fragment_2"

        android:layout_row="1"
        android:layout_column="1"
        android:layout_marginEnd="5dp"
        android:layout_marginTop="5dp"
        tools:layout="@layout/fragment_layout" /> 


</GridLayout>

所以,我写了片段代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyFragment">
android:background="@drawable/back">

<GridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|top"
    android:columnCount="3"
    android:rowCount="11">

    <TextView
        android:layout_width="45dp"
        android:layout_height="72dp"
        android:id="@+id/tvView"
        android:text="0"
        android:layout_gravity="right|center_vertical"
        android:layout_row="4"
        android:layout_column="0" />

    <TextView
        android:layout_width="40dp"
        android:layout_height="85dp"
        android:text="Some Content"
        android:id="@+id/tvName"
        android:layout_gravity="center_horizontal|top"
        android:layout_row="4"
        android:layout_column="2" />

    <EditText
        android:layout_width="80dp"
        android:layout_height="107dp"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/etText"
        android:layout_gravity="left|center_vertical"
        android:text="0"
        android:layout_row="8"
        android:layout_column="0" />

    <ImageButton
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/ibButton"
        android:background="#b69292"
         />
</GridLayout>

但是,当我运行应用程序时,它看起来非常糟糕:图像按钮看起来非常小。 据我所知,ImageButton的宽度为50dp?或者,宽度会随着活动中片段的大小而变化?

如何在活动中制作片段布局? 也许,你有关于这个案例的一些教程链接吗?

2 个答案:

答案 0 :(得分:3)

您可以使用framelayout而不是使用片段布局,然后您可以在您的活动中写下它:

YourFragment yourFragment = new YourFragment();
    getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, yourFragment).commit();

此处,frame_layout是活动的xml文件中的FrameLayout标记。 这更容易。

答案 1 :(得分:1)

为了让您的应用程序在不同的屏幕尺寸上运行,您应该定义具有通用尺寸的视图,例如wrap_contentmatch_parent,这将有助于使您的应用程序对于不同的屏幕尺寸更具动态性。

这是一个简单的例子:
 http://developer.android.com/guide/topics/ui/declaring-layout.html#write