繁体   English   中英

在Android中定义片段布局的高度和位置

[英]define height and position of fragment layout in android

我在Android布局中使用片段。 如何设置片段布局的高度?

这是activity_main.xml

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Fragment No.1"
    android:onClick="selectFrag" />

 <Button
     android:id="@+id/button2"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:onClick="selectFrag"
     android:text="Fragment No.2" /> 

  <fragment
    android:name="com.android.fragmentstest.FragmentOne"
    android:id="@+id/fragment_place"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />

 </LinearLayout>

这是fragment.xml

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="40dp"
    android:layout_height="20dp"
    android:id="@+id/tutlist_fragment"
    android:layout_weight="45"
    android:background="#7bae16">


   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="50dp"
       android:layout_weight="1"
       android:text="This is fragment No.1"
       android:textStyle="bold" />

       </RelativeLayout>

我需要设置片段的最大高度和片段的位置。 我将如何实现?

我看不出最大的问题是什么。 可以像其他任何视图一样设置Fragment的属性(例如:height,weight等)

因此,设置片段高度的正确方法是

这将是activity_main.xml文件。您无需在各个片段本身中定义布局高度。

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Fragment No.1"
        android:onClick="selectFrag" />

     <Button
         android:id="@+id/button2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:onClick="selectFrag"
         android:text="Fragment No.2" /> 

   <fragment
       android:id="@+id/fragment_place"
       android:name="com.android.fragmentstest.FragmentOne"
       android:layout_width="match_parent"
       android:layout_height="90dp" />

</LinearLayout>

请注意, Android:layout_height =“ 90dp”定义了主活动中片段的高度。

暂无
暂无

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

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