0
私はAndroidが初めてで、ヘルプが必要です。Androidどのように設定するViewのデフォルトの背景色
animeList.setBackgroundColor(Color.BLUE);
なぜレイアウトの背景に影響しませんか?クリックイベントのイベントは正常に機能します。私はTextViewを試したが、何も変わっていない。ありがとう...
package com.animeworld.tenshi;
import android.app.Activity;
import android.graphics.Color;
import android.media.Image;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class AnimeCategoryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_anime_category);
ArrayList<Anime> animes = new ArrayList<>();
Anime[] listOfAnime = Anime.animes;
for(Anime loa : listOfAnime) {
animes.add(loa);
}
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.activity_anime_category);
for(Anime anime : animes) {
final LinearLayout animeList = new LinearLayout(this);
LinearLayout.LayoutParams animeListLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
animeListLayout.setMargins(0, 0, 0, 5);
animeList.setLayoutParams(animeListLayout);
animeList.setBackgroundColor(Color.BLUE);
animeList.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
animeList.setBackgroundColor(Color.parseColor("#7CCAFA"));
break;
case MotionEvent.ACTION_UP:
animeList.setBackgroundColor(Color.WHITE);
break;
}
return true;
}
});
ImageView animeListImg = new ImageView(this);
LinearLayout.LayoutParams animeListImgLayout = new LinearLayout.LayoutParams(200, 300);
animeListImgLayout.setMargins(0, 0, 5, 0);
animeListImg.setLayoutParams(animeListImgLayout);
TextView animeListDesc = new TextView(this);
LinearLayout.LayoutParams animeListDescLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
animeListDesc.setLayoutParams(animeListDescLayout);
animeListDesc.setTextColor(Color.BLUE);
animeListDesc.setGravity(Gravity.CENTER);
animeListDesc.setTextSize(18);
animeList.addView(animeListImg);
animeList.addView(animeListDesc);
animeListImg.setImageResource(anime.getImageResourceId());
animeListDesc.setText(anime.getName());
mainLayout.addView(animeList);
}
}
}