我是Android新手,我需要帮助。
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);
}
}
}
答案 0 :(得分:0)
您正在"mainLayout"
容器内动态添加布局。因此,您必须将背景颜色设置为父视图/容器视图。