将Toolbar添加到活动时,CardView项目之间存在较大差距

时间:2018-03-07 04:47:48

标签: android android-recyclerview android-toolbar android-cardview

我有一个活动显示搜索音乐家的结果,当我将自定义工具栏添加到XML时,它会在每个CardView元素之间显示这些大条。我不确定导致这种情况的原因是当我删除工具栏时,它看起来很好。

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.Dark"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ></android.support.v7.widget.RecyclerView>


<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/colorSplash"
    app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:orientation="vertical"
        android:padding="@dimen/activity_horizontal_margin">

        <TextView
            android:id="@+id/artistName"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"

        android:layout_gravity="left"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/artistImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left" />


    </LinearLayout>


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

活动:

public class SearchResultsActivity extends AppCompatActivity {

private List<Artist> artistList;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    List<searchItem> results = new ArrayList<>();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_results_activity);
    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar2);
    setSupportActionBar(mToolbar);

    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    artistList = handleIntent(getIntent());
    for(int i=0; i< artistList.size();i++)
    {
        String name = artistList.get(i).getName();
        String image = artistList.get(i).getImageURL(ImageSize.LARGE);
        searchItem item = new searchItem(name, image);
        results.add(item);
    }



    adapter = new searchItemAdapter(results, getApplicationContext());
    recyclerView.setAdapter(adapter);
}



@Override
protected void onNewIntent(Intent intent) {

    handleIntent(intent);
}

private List<Artist> handleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        String test = query;
        try {
            artistList = new searching2().execute(query).get();
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
    return artistList;
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater mMenuInflater = getMenuInflater();
    mMenuInflater.inflate(R.menu.mainmenu, menu);
    SearchView search = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menuSearch));

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    search.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, SearchResultsActivity.class)));
    search.setQueryHint(getResources().getString(R.string.search_hint));
    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item){

    //popup for about
    if(item.getItemId() == R.id.action_about_us){
        PopupWindow popup = new PopupWindow(this);
        View layout = getLayoutInflater().inflate(R.layout.about_popup, null);
        popup.setContentView(layout);
        popup.setOutsideTouchable(true);
        popup.setFocusable(true);
        popup.showAtLocation(layout, Gravity.CENTER,0,0);
    }
    //go to sign in page
    else if(item.getItemId() == R.id.action_sign_in){

        Intent intent = new Intent(this, LoginActivity.class);
        this.startActivity(intent);
    }

    return super.onOptionsItemSelected(item);
}
}
class searching2 extends AsyncTask<String, Void, List<Artist>>
{

@Override
protected List<Artist> doInBackground(String...query)
{
    String test2 = query[0];
    Caller.getInstance().setUserAgent("tst");
    List<Artist> test = new ArrayList<Artist>(Artist.search(test2, "5bd9e71b5242c6eb80f7be611d05ac34"));
    return test;
}
}

工具栏的外观如何:

enter image description here

没有工具栏时的外观

enter image description here

0 个答案:

没有答案