我有一个自定义视图,我把它放在一起,看起来像 Google Now 卡片。这是它在 Android Studio 中的样子:
忽略 Android Studio 在其周围渲染屏幕其余部分的事实;视图只是它底部的阴影所包围的矩形。如果我将其设置为主要内容视图,并在运行时膨胀一个,它看起来是正确的。如果我在运行时将一个作为 ListView 的一部分进行充气,它看起来像这样:
消失的视图似乎有一个共同点,那就是它们附加到布局边缘以外的东西上。这是布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="340dp"
android:layout_height="260dp"
android:clipToPadding="true"
android:background="@drawable/card_background"
android:minHeight="260dp"
android:minWidth="340dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="4"
android:id="@+id/textBeat"
android:fontFamily="sans-serif-light"
android:textSize="80sp"
android:textColor="@android:color/holo_red_light"
android:layout_above="@+id/linearLayout"
android:layout_alignRight="@+id/buttonMenu"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Card Title"
android:id="@+id/textTitle"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:textColor="@android:color/darker_gray"
android:fontFamily="sans-serif-light"
android:maxWidth="280dp"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:textSize="36sp"
android:maxHeight="96dp"
android:ellipsize="end"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/linearLayout">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/buttonToggle"
android:layout_weight="1"
android:background="@android:color/transparent"
android:src="@drawable/ic_launcher"/>
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@android:color/darker_gray"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/buttonDelete"
android:layout_weight="1"
android:background="@android:color/transparent"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:background="@android:color/transparent"
android:onClick="showPopup"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="12dp"
android:layout_marginRight="12dp"
android:id="@+id/buttonMenu"/>
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="200"
android:id="@+id/buttonTempo"
android:layout_above="@+id/linearLayout"
android:layout_alignLeft="@+id/textTitle"
android:background="@android:color/transparent"
android:textColor="@android:color/holo_blue_light"/>
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="4"
android:id="@+id/buttonSignature"
android:background="@android:color/transparent"
android:textColor="@android:color/holo_blue_light"
android:layout_above="@+id/linearLayout"
android:layout_toRightOf="@+id/buttonTempo"/>
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="2"
android:id="@+id/buttonSubdiv"
android:background="@android:color/transparent"
android:textColor="@android:color/holo_blue_light"
android:layout_above="@+id/linearLayout"
android:layout_toRightOf="@+id/buttonSignature"/>
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/iconTempo"
android:layout_above="@+id/buttonTempo"
android:layout_alignLeft="@+id/buttonTempo"
android:src="@drawable/ic_launcher"
android:paddingTop="8dp"/>
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/iconSignature"
android:src="@drawable/ic_launcher"
android:layout_above="@+id/buttonTempo"
android:layout_toRightOf="@+id/buttonTempo"
android:paddingTop="8dp"/>
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/iconSubdiv"
android:src="@drawable/ic_launcher"
android:layout_above="@+id/buttonSignature"
android:layout_toRightOf="@+id/buttonSignature"
android:paddingTop="8dp"/>
这是适配器:
public class CardAdapter extends BaseAdapter {
private static final String TAG = "RhythmCards";
private final Context mContext;
private final ArrayList<Card> mCards;
public CardAdapter(Context context, ArrayList<Card> cards) {
mContext = context;
mCards = cards;
}
static class ViewHolder {
public TextView title;
public Button tempo;
public Button signature;
public Button subdivision;
public TextView beat;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ViewHolder holder;
// If the row passed in is null, we must inflate and populate a new view.
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.card_layout, null);
holder = new ViewHolder();
// Put references to views contained in card into holder
holder.title = (TextView) convertView.findViewById(R.id.textTitle);
holder.tempo = (Button) convertView.findViewById(R.id.buttonTempo);
holder.signature = (Button) convertView.findViewById(R.id.buttonSignature);
holder.subdivision = (Button) convertView.findViewById(R.id.buttonSubdiv);
holder.beat = (TextView) convertView.findViewById(R.id.textBeat);
convertView.setTag(holder);
} else {
// Otherwise, we use the holder attached to the row passed in
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
@Override
public int getCount() {
return mCards.size();
}
@Override
public Object getItem(int position) {
return mCards.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
是什么阻止了这些视图的出现?