简体   繁体   English

如何在活动中进行片段布局?

[英]How to make fragment layout inside activity?

How to make activity and fragment layouts?如何制作活动和片段布局?

I have main activity layout, wich contains 4 fragments (they are staggered).我有主要活动布局,其中包含 4 个片段(它们交错排列)。 I write two of them.我写了其中的两个。 They are similar.他们很相似。

<?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>

So, i write code of fragment:所以,我写了片段代码:

<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>

But, when i run app- it looks very bad: Image button looks very small.但是,当我运行应用程序时,它看起来很糟糕:图像按钮看起来很小。 As i understand, width of ImageButton will be 50dp?据我了解, ImageButton宽度将为 50dp? Or, width will vary with respect to size of the fragment in the activity?或者,宽度会随着活动中片段的大小而变化?

How to make fragment layout inside activity?如何在活动中进行片段布局? Maybe, you tak eme some tutorial links about this case?也许,你拿了一些关于这个案例的教程链接?

Instead of using fragment layout, you can use framelayout and then you can write this in your activity:您可以使用 framelayout 而不是使用片段布局,然后您可以在您的活动中编写它:

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

Here, frame_layout is the FrameLayout tag in your activity's xml file.此处,frame_layout 是活动的 xml 文件中的 FrameLayout 标记。 This is much easier.这要容易得多。

In order for your application to run on different screen sizes, you should define views with generic sizes such as wrap_content and match_parent , this will help in making your application more dynamic for different screen sizes.为了让您的应用程序在不同的屏幕尺寸上运行,您应该定义具有通用尺寸的视图,例如wrap_contentmatch_parent ,这将有助于使您的应用程序针对不同的屏幕尺寸更加动态。

Here is a simple example for that:这是一个简单的例子:
http://developer.android.com/guide/topics/ui/declaring-layout.html#write http://developer.android.com/guide/topics/ui/declaring-layout.html#write

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM