【问题标题】:Customise Textview of included layout自定义包含布局的 Textview
【发布时间】:2015-01-15 10:32:12
【问题描述】:

我的布局如下:

<?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>

这给出了以下结果:

问题是我需要访问每个&lt;include&gt; 中的文本视图和图像视图。因此徽标图像会有所不同,测试徽标的详细信息会有所不同,并且每个徽标的“x10”都会有所不同,因为我希望从数据库中填充它们。有人可以对此有所了解吗?

我本来打算为此使用视图寻呼机,但这不起作用,因为它占据了屏幕的整个宽度和高度。

【问题讨论】:

    标签: android android-layout include android-viewpager horizontal-scrolling


    【解决方案1】:
    <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);
    

    【讨论】:

    • 感谢您的回复,听起来很愚蠢,如何通过向包含添加 ID 来访问每个包含的 textview/imageviews?如果您不介意解释,我将不胜感激
    • @DJ-DOO 检查我更新的答案。 Rachamani 也给出了我认为相同的答案。你可以去看看。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      • 2019-08-03
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2015-01-28
      • 1970-01-01
      相关资源
      最近更新 更多