【问题标题】:Can we have image sliderview and icons in one activity?我们可以在一个活动中包含图像滑块视图和图标吗?
【发布时间】:2016-03-16 15:01:15
【问题描述】:

我正在尝试为看起来像这张图片的活动编写代码。在我的屏幕顶部,我想要一个图像滑块视图以及滑块视图下方的一些图标,如图所示。我该怎么做?

.

【问题讨论】:

    标签: android


    【解决方案1】:

    我建议为您的目的使用this 库。只需添加它的依赖项并将其设置为巡演活动的顶部,然后用图标或您想要的任何其他内容填充其余部分。

    【讨论】:

      【解决方案2】:

      这是我作为一个应用程序的教程所做的:我已经在中心加载了图像,我不是用指针而是用相对布局内的“Go”和“Back”按钮来改变它们以移动图像, 但您可以随意更改。我有一个带有“完成”的居中按钮和一个底部有一个标题的标签。 正如您将看到的,这是一个没有预览或图标的示例,但也许这是您可以自己开始的代码。

      教程.xml

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".Notificacion" >
      
      <ImageView
          android:id="@+id/imageView1"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_above="@+id/button3"
          android:layout_below="@+id/RelativeLayout1"
          android:layout_marginTop="4dp"
          android:src="@drawable/guia1" />
      
      <TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="8dp"
          android:layout_marginTop="8dp"
          android:text="@string/img1"
          android:textColor="#000000"
          android:textSize="15sp" />
      
      <RelativeLayout
          android:id="@+id/RelativeLayout1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:orientation="horizontal" >
      
          <Button
              android:id="@+id/button_back"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_alignParentTop="true"
              android:background="@android:color/transparent"
              android:paddingLeft="6dp"
              android:text="@string/back"
              android:textColor="#088A08"
              android:textSize="15sp" />
      
          <Button
              android:id="@+id/button_next"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true"
              android:layout_alignParentTop="true"
              android:background="@android:color/transparent"
              android:paddingRight="8dp"
              android:text="@string/next"
              android:textColor="#088A08"
              android:textSize="15sp" />
      
      </RelativeLayout>
      
          <Button
              android:id="@+id/button3"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_above="@+id/textView1"
              android:layout_centerHorizontal="true"
              android:background="@android:color/transparent"
              android:onClick="back"
              android:text="@string/fin"
              android:textColor="#088A08"
              android:textSize="12sp" />
      </RelativeLayout>
      

      我的类名为 Tutorial.java,它实现了 OnClickListener。在这里,我从 drawable 文件夹中加载了几张图片,并为每张图片加载了一个文本。

      import android.os.Bundle;
      import android.app.Activity;
      import android.content.Intent;
      import android.view.Menu;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.ImageView;
      import android.widget.TextView;
      
      public class Tutorial extends Activity implements OnClickListener{
      
      ImageView foto;
      TextView tv;
      int[] fotoId = {R.drawable.yourimage01, R.drawable.yourimage02, R.drawable.yourimage03, R.drawable.yourimage04, R.drawable.yourimage05, R.drawable.yourimage06, R.drawable.yourimage07}; 
      String[] textos = {"Text for image 01", "Text for image 02", "Text for image 03", "Text for image 04", "Text for image 05", "Text for image 06", "Text for image 07",};
      int i = 0;
      int total;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.tutorial);
      
          Button next= (Button) findViewById(R.id.button_next);
          Button back= (Button) findViewById(R.id.button_back);
          next.setOnClickListener(this);
          back.setOnClickListener(this);
      
          tv = (TextView) findViewById(R.id.textView1);
          foto = (ImageView) findViewById(R.id.imageView1);
          total = fotoId.length;
      }
      
      @Override
      public void onClick(View view) {
      
          int id = view.getId();
          if(id == R.id.button_back) {
              i++;
              if(i == total){
                  i = 0;
              }
          }
      
          if(id == R.id.button_next) {
              i--;
              if(i == -1){
                  i = total -1;
              }
          }
          foto.setImageResource(fotoId[i]);
          tv.setText(textos[i]);
      
      }
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
      
      public void back(View view) {
          Intent i = new Intent(this, Indice.class );
          startActivity(i);
        }
      }
      

      希望对你有帮助。

      【讨论】:

      • 这两个答案都对我帮助很大。但是,亲爱的朋友 Xose Sanchez,在您的代码中有一个名为 indice 的类,我们将在该类中拥有它,因为它在我尝试您的代码时显示错误。你能帮帮我吗?
      • 哦,是的,对不起。这是我用后退按钮调用的另一个活动,这个应用程序的索引和主要活动;您可以更改您调用的活动,甚至在不需要时删除该按钮。它只是实现了背面;如果您不要忘记删除 .xml 上的按钮
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 2011-04-21
      相关资源
      最近更新 更多