【问题标题】:inflate another layout below in android在android下面膨胀另一个布局
【发布时间】:2014-09-19 20:08:35
【问题描述】:

这是我的帖子形状 xml 文件

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/layout_bg"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:layout_margin="10dp"
    android:id="@+id/hiddenLayout">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/rsz_page_pic"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/imageView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:id="@+id/pageNameTV"
        android:layout_toStartOf="@+id/imageView"
        android:paddingRight="10dp"
        android:layout_alignTop="@+id/imageView"
        android:layout_toLeftOf="@+id/imageView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="asdasdsd"
        android:id="@+id/postTimeTV"
        android:paddingRight="10dp"
        android:textColor="@color/normal_end"
        android:layout_alignBottom="@+id/imageView"
        android:layout_alignRight="@+id/pageNameTV"
        android:layout_alignEnd="@+id/pageNameTV" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:id="@+id/textView3"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/imageView"
        android:layout_alignRight="@+id/imageView"
        android:layout_alignEnd="@+id/imageView" />

</RelativeLayout>

我的提要片段

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="23dp"
    android:paddingRight="13dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/feeds_container">

    </RelativeLayout>

</ScrollView>

我的代码是

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        mRootView = inflater.inflate(R.layout.fragment_feed, container, false);

        RelativeLayout hiddenLayout = (RelativeLayout)getActivity().findViewById(R.id.hiddenLayout);

        RelativeLayout myLayout = (RelativeLayout)mRootView.findViewById(R.id.feeds_container);
        View hiddenInfo = getActivity().getLayoutInflater().inflate(R.layout.post_shape, myLayout, false);
        myLayout.addView(hiddenInfo);

        hiddenInfo = getActivity().getLayoutInflater().inflate(R.layout.post_shape, myLayout, false);
        myLayout.addView(hiddenInfo);

        return mRootView;
    }

我想要做的是将 post_shape.xml 的副本复制到 feed_fragment.xml 中,第一个是正确的,但是当我尝试在第一个下面添加另一个时,我得到了这个 http://s14.postimg.org/p8sh0yiq9/ant.jpg 我真的很讨厌我的英语不好,如果你看到图片,我希望你能理解我的问题

【问题讨论】:

  • 使用线性布局而不是相对

标签: android xml android-layout layout-inflater android-inflate


【解决方案1】:

这里有 2 个选项... 1.将文件Feed Fragment中的RealtiveLayout替换为LinearLayout(LinearLayout也必须占据整个屏幕,因此它会将它们一个接一个地插入) 2. ScrollView 和RelativeLayout 的实例使用ListView 和Adapter 在这里非常有用。

ListView Example

【讨论】:

    猜你喜欢
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多