【问题标题】:define height and position of fragment layout in android在android中定义片段布局的高度和位置
【发布时间】:2014-07-04 08:22:06
【问题描述】:

我在我的 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>

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

【问题讨论】:

  • 你想达到什么目的??你的问题不清楚..

标签: android xml android-layout user-interface android-fragments


【解决方案1】:

我看不出有什么大问题。可以像任何其他视图一样设置 Fragment 的属性(例如:高度、重量等)

所以,设置片段高度的正确方法是

这将是 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" 定义了主要活动中片段的高度。

【讨论】:

  • 谢谢。正是我需要知道的。
  • 如果片段的内容是可变大小并且片段需要是wrap_content怎么办?在 xml 声明中设置 wrap_content 似乎不起作用。片段只是没有显示。
猜你喜欢
  • 2013-06-01
  • 2023-03-14
  • 1970-01-01
  • 2013-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多