【发布时间】:2018-04-17 13:58:24
【问题描述】:
我正在开发一个音乐播放器应用程序,并且我创建了一个 FrameLayout,它用作一张“卡片”,显示我正在尝试添加的每首歌曲的信息(专辑封面、标题、艺术家等)这张卡片中的复选框,我计划对其进行自定义以创建赞成和反对按钮。但是,卡片中的复选框不会响应点击。如果我在应用程序的主要活动中绘制一个复选框(我正在绘制卡片),它工作正常。
这里是卡片视图(“MusicCardView”)的onDraw()方法:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw the voting controls
try {
CheckBox upvoteButton = new CheckBox(getContext());
upvoteButton.setX(20);
upvoteButton.setTop(-60);
upvoteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Is the button now checked?
Log.d("MainActivity", "upvote clicked");
}
});
CheckBox downvoteButton = new CheckBox(getContext());
downvoteButton.setX(20);
downvoteButton.setTop(-150);
downvoteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Is the button now checked?
Log.d("MainActivity", "downvote clicked");
}
});
upvoteButton.draw(canvas);
downvoteButton.draw(canvas);
} catch (Exception e) {
Log.e("MusicCardView", "exception", e );
}
}
主要活动的xml(“PartyActivity”):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.musique.PartyActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main_linear_view"
android:layout_marginBottom="56dp">
<com.musique.MusicCardView
android:layout_width="match_parent"
android:id="@+id/now_playing_view"
android:layout_height="80dp"
android:background="#ccc"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
app:exampleColor="#000000"
app:songTitle="Money"
app:artist="Pink Floyd"
app:album="The Dark Side of the Moon"
app:exampleDimension="18sp"
app:exampleDrawable="@drawable/darkside"
app:exampleString="example" />
<Space
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="@+id/controller_space"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/query"
android:layout_gravity="center"
android:layout_marginTop="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/searchBtn"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Search"
android:background="#5eca99"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvData"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:animateLayoutChanges="true"
android:id="@+id/main_scroll_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/scroll_child">
</LinearLayout>
</ScrollView>
</LinearLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
MusicCardView 是 PartyActivity 中的一个示例
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MusicCardView v = (MusicCardView) vi.inflate(R.layout.music_card_template, null);
v.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 210));
LinearLayout linearLayout = findViewById(R.id.scroll_child);
v.setSongAttributes(song);
new RetrieveArtTask(v).execute(song);
Log.d("PartyActivity", "SongAttributes Set");
linearLayout.addView(v);
我查看了this question 和this question,但对发布的解决方案没有任何帮助,如果您提供任何建议,我将不胜感激。
【问题讨论】:
-
upvoteButton.draw(canvas);这是错误的:您必须使用ViewGroup#addView而不是“绘制”您的复选框,它们会自己绘制并响应您的点击 -
好吧,我不是在回答你的问题,而是想知道你会在哪里使用 upArrow 和 downArrow drawable?
-
@AJay 哎呀,那是旧代码。已编辑删除。