【问题标题】:How to display an image in place of Toolbar layout in Master/Detail flow (Android Studio)?如何在主/详细信息流(Android Studio)中显示图像代替工具栏布局?
【发布时间】:2016-02-07 07:49:13
【问题描述】:

我已经开始使用主/详细信息流创建应用程序。我想在工具栏布局中为列表中的不同项目显示不同的图像。谁能取悦我。

【问题讨论】:

    标签: android android-layout master-detail


    【解决方案1】:

    我假设您使用了 Android Studio 生成的主/详细信息活动。 在主要活动中,您可以找到从该资源“item_list”膨胀的回收视图。 您可以为其覆盖适配器。

    有很多关于回收视图的好教程。例如http://www.vogella.com/tutorials/AndroidRecyclerView/article.html

    【讨论】:

      【解决方案2】:

      感谢 Lollipop,工具栏实际上只是另一种布局。因此,您可以使用您想要的任意数量的视图组件(包括图像)定义一个布局,然后将向导生成的工具栏替换为您创建的自定义工具栏。

      这是一个示例 custom_toolbar.xml

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.v7.widget.Toolbar 
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/custom_toolbar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="0dp"
          android:background="@color/primary"
          android:padding="0dp"
          app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar"
          app:theme="@style/AppTheme.PopupOverlay" >
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical" >
      
          <TextView
              android:id="@+id/text_view_profile_name"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:gravity="center_horizontal"        
              android:text="John Doe"
              android:textStyle="bold" />
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
               android:orientation="horizontal" >
      
              <ImageView
                  android:id="@+id/profile_image"
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="72dp"
                  android:src="@drawable/John_doe_image.jpg"
                  android:layout_marginRight="4dp"
                   />
      
                   <TextView
                      android:id="@+id/profile_name"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="john_doe@gmail.com"
                      android:textStyle="bold"
                      android:textSize="25sp"
                      android:layout_gravity="center_horizontal"/>
      
              </LinearLayout>   
          </LinearLayout>
      
      </android.support.v7.widget.Toolbar>
      

      有了这个,您现在可以转到包含您要替换的工具栏的布局。删除该工具栏并将其替换为您的自定义工具栏,如下所示

       <include layout="@layout/custom_toolbar"
      

      希望对您有所帮助。 谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-08
        • 1970-01-01
        • 2010-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多