自定义包含布局的Textview

4

我有一个如下的布局:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:id="@+id/logo_background"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/logos_background">
        </ImageView>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="5dp"
            android:layout_alignBottom="@+id/logo_background"
            >
            <TextView
                android:id="@+id/logoDetails"
                android:text="Test Logo details"
                android:textSize="15sp"
                android:layout_centerHorizontal="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <TextView
                android:id="@+id/stockQuantity"
                android:layout_centerHorizontal="true"
                android:textSize="20sp"
                android:layout_below="@+id/logoDetails"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"    
                android:text="x10"/>

        </RelativeLayout>
    </RelativeLayout>    
</RelativeLayout>

我需要在另一个布局中多次包含此布局,我已按以下方式完成:

<HorizontalScrollView
    android:layout_below = "@+id/selectLayout"
    android:id="@+id/pager"
    android:scrollbars="none"
    android:layout_marginLeft="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <include layout="@layout/viewpager_item_layout"/>
        <include layout="@layout/viewpager_item_layout"/>
        <include layout="@layout/viewpager_item_layout"/>
        <include layout="@layout/viewpager_item_layout"/>
        <include layout="@layout/viewpager_item_layout"/>
        <include layout="@layout/viewpager_item_layout"/>
        <include layout="@layout/viewpager_item_layout"/>
    </LinearLayout>
</HorizontalScrollView>

这将产生以下结果: enter image description here 问题是我需要访问每个中的文本视图和图像视图。 因此,每个标志图像、测试标志详细信息和“x10”对于每个标志都将不同,因为我希望从数据库中填充它们。 有人能为此提供帮助吗?
我原本打算使用view pager来实现这一点,但是它会占据整个屏幕的宽度和高度,所以行不通。
2个回答

5
<include android:id="@+id/news_title"
         layout="@layout/viewpager_item_layout"/>

尝试看是否有帮助。

您可以这样访问:

View includedLayout = findViewById(R.id.news_title);
TextView insideTheIncludedLayout = (TextView )includedLayout.findViewById(R.id.logoDetails);

谢谢您的回复。请问,如果我给每个include添加一个ID,如何访问我的textview/imageviews?如果您能解释一下,我会非常感激。 - DJ-DOO
1
@DJ-DOO,请查看我的更新答案。Rachamani也给出了相同的答案,你可以去看看。 - Android Killer

1

在将布局设置为include标签后,在您的代码中使用以下内容:

   View v = findViewById(R.id.your_layout_id);
   TextView tv = (TextView) v.findViewById(R.id.text_view_id);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接