改变布局的组成部分

时间:2017-09-26 16:18:27

标签: java android xml

我是android的初学者,我有gridview个图片,当你点击图片时,它会将你带到SecondActivity。在SecondActivity我有一些与每个图像有关的信息。 SecondActivity的布局对于所有图像都是相同的,我想在同一布局中更改每个图像的标题,文本和图像。 我使用了setTitlesetTextsetImageDrawable(),但它没有改变。我会提到我的代码中的一些行,也许它会完全错误,所以如果是,请引导我找一个简单的来源。

产品类

public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
MainActivity中的

public List<Product> getProductList() {
        //pseudo code to get product, replace your code to get real product here
        productList = new ArrayList<>();
        productList.add(new Product(R.drawable.ii1, " Story Name", "This is description 1"));
        productList.add(new Product(R.drawable.i2, "Story Name", "This is description 2"));
        productList.add(new Product(R.drawable.i3, "Story Name", "This is description 3"));
        productList.add(new Product(R.drawable.i4, "Story Name", "This is description 4"));
        productList.add(new Product(R.drawable.i5, "Story Name", "This is description 5"));
        productList.add(new Product(R.drawable.i6, "Story Name", "This is description 6"));
        productList.add(new Product(R.drawable.i8, "Story Name", "This is description 8"));
        productList.add(new Product(R.drawable.i9, "Story Name", "This is description 9"));
        productList.add(new Product(R.drawable.i10, "Story Name", "This is description 10"));

        return productList;
    }
SecondActivity中的

 mToolbar = findViewById(R.id.toolbar);
        img  = findViewById(R.id.imageView6);
         t=findViewById(R.id.textView4);
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            mToolbar.setTitle(bundle.getString("title"));

            if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
                img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.ii1));
                t.setText(bundle.getString("title"));
            }

            else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")){
                img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i2));

            }

            else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
                img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i3));

            }

            else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
                img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i4));

            }

            else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
                img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i5));

            }

            else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
                img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i6));

            }

            else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
                img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i8));

            }

            else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
                img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i9));

            }

            else if (mToolbar.getTitle().toString().equalsIgnoreCase("Story Name")) {
                img .setImageDrawable(ContextCompat.getDrawable(SecondActivity.this, R.drawable.i10));

            }


        }
    }

}

第二个活动XML

<ImageView
        android:id="@+id/imageView6"
        android:layout_width="160dp"
        android:layout_height="180dp"
        android:layout_marginLeft="13dp"
        android:layout_marginStart="13dp"
        android:layout_marginTop="60dp"
        app:srcCompat="@drawable/ii1"
        tools:ignore="ContentDescription"
        android:layout_below="@+id/appBarLayout"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView6"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="22dp"
        android:layout_toRightOf="@+id/imageView6"
        android:textSize="14sp"
        tools:ignore="HardcodedText,RtlHardcoded" />

2 个答案:

答案 0 :(得分:0)

单击一个图像时,转到单击事件并保持图像的位置。然后将列表中的信息发送到SecondActivity。

答案 1 :(得分:0)

现在正在与我合作 我将文本发送到第二个活动

<强> MainActivity

Intent i= new Intent(MainActivity.this,SecondActivity.class);
                    Bundle b= new Bundle();
                    b.putString("Story Name", productList.get(position).getTitle().toString());
                    i.putExtras(b);
                    startActivity(i);

<强> SecondActivity

TextView t =findViewById(R.id.textView4);
Bundle b=getIntent().getExtras();
String story=b.getString("Story Name");
t.setText(story);

我也在图像和标题上使用它