卡片视图的布局高度不起作用?

时间:2018-03-30 11:30:55

标签: android

我试图制作 layout_height=="match_parent" 的卡片视图,但卡片的高度绝不是 match_parent ,它是'始终 wrap_content

我尝试了将RelativeLayout转换为LinearLayout甚至FrameLayout的所有排列,以用于我的activity_main,但没有任何效果。请告诉我如何将它的大小增加到match_parent

list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
card_view:cardCornerRadius="2dp"
card_view:contentPadding="10dp"
android:layout_margin="8dp"
android:background="?android:selectableItemBackground">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="4dp"
    android:orientation="vertical">


    <ImageView
        android:id="@+id/sportsImage1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"/>


    <TextView
        android:id="@+id/newsTitle1"
        style="@style/TextAppearance.AppCompat.Headline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/subTitle1"
        style="@style/TextAppearance.AppCompat.Subhead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp" />


</LinearLayout>

</android.support.v7.widget.CardView>
</RelativeLayout>

我无法在 list_item.xml 文件中删除包含card_view的相对布局,因为如果我这样做,则card_view会丢失其阴影并看起来像普通视图。

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@android:color/transparent"
    android:id="@+id/list"
/>
</RelativeLayout>

我尝试删除 activity_main 中的相对布局,但这也没有用。

  

MainActivity.java

    public class MainActivity extends AppCompatActivity {

String title="Bitfinex Confirms it Will not List the Petro Cryptocurrency";
String cont="It is evident the Venezuelan Petro is a pretty intriguing currency.";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<News_item> arrayList=new ArrayList<News_item>();
    arrayList.add(new News_item(R.drawable.img_badminton,title,cont));
    arrayList.add(new News_item(R.drawable.img_baseball,title,cont));
    arrayList.add(new News_item(R.drawable.img_basketball,title,cont));
    arrayList.add(new News_item(R.drawable.img_bowling,title,cont));
    arrayList.add(new News_item(R.drawable.img_cycling,title,cont));
    arrayList.add(new News_item(R.drawable.img_golf,title,cont));
    arrayList.add(new News_item(R.drawable.img_running,title,cont));
    arrayList.add(new News_item(R.drawable.img_soccer,title,cont));

    News_adapter adapter=new News_adapter(this,arrayList);
    ListView listView=(ListView) findViewById(R.id.list);
    listView.setAdapter(adapter);

}
}

ArrayAdapter

   public class News_adapter extends ArrayAdapter<News_item> {
Context mContext;
public News_adapter(@NonNull Context context, ArrayList<News_item> resource) {
    super(context,0,resource);
    mContext=context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.layout, parent, false);
    }
    News_item news_item=getItem(position);
    TextView textView=(TextView) listItemView.findViewById(R.id.newsTitle1);
    TextView textView2=(TextView) listItemView.findViewById(R.id.subTitle1);
    ImageView imageView=(ImageView) listItemView.findViewById(R.id.sportsImage1);

    Glide.with(mContext).load(news_item.getImage()).into(imageView);
    textView.setText(news_item.getHeading());
    textView2.setText(news_item.getBody());


    return listItemView;
}

}

1 个答案:

答案 0 :(得分:0)

使用app:cardUseCompatPadding="true",您不必将RelativeLayout用作父级。这将为卡片添加足够的空间以保持阴影显示。