【问题标题】:inflating views android膨胀视图android
【发布时间】:2011-07-24 21:21:37
【问题描述】:

我有这个 main.xml 布局,它有一个固定的页眉和页脚。正文内容必须根据接收到的参数进行更改。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="#FFFFFF"
    android:id="@+id/main_view">
    <com.HeaderView
        android:id="@+id/HeaderView"
         android:layout_alignParentTop="true"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />
    <com.FooterView
        android:id="@+id/FooterView"
        android:layout_alignParentBottom="true" android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/BodyView"
        android:layout_below="@id/HeaderView"
        android:layout_above="@id/FooterView"/>
</RelativeLayout>

基于以下条件,我需要在 main.xml 中加载 BodyView。

If parameter == Apple 
  then load layout AppleView in the BodyView
if parameter == Bat
  then load layout BatView in the BodyView

我定义了 AppleView 和 BatView xml 布局。有人可以指导我如何加载任何一个视图并在其中设置/获取值。

【问题讨论】:

    标签: android android-layout android-inflate


    【解决方案1】:

    这是一个充气的例子,希望这是你需要的。

      LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflater.inflate(R.layout.temp, null);
            RelativeLayout rl = (RelativeLayout)findViewById(R.id.glavenfrejm);
            RelativeLayout.LayoutParams parametri = new  RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
            parametri.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            rl.addView(v, parametri);
    

    【讨论】:

    • 很抱歉,我仍然无法弄清楚如何使用这段代码。能否请您详细说明
    • 每次测试后(每次 if 后)你都这样做(做布局的膨胀。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多