Android新闻应用程序UI在gridview顶部显示空白空间?

时间:2018-10-12 15:18:00

标签: android user-interface

我希望你们所有人都做得很好,我正在开发新闻应用程序,但顶部的卡片视图上有空白,我从小就以卡片视图作为父约束布局。

此屏幕截图Emulator

public Object[][] getData(Class clazz) {
        ClassLoader classLoader = getClass().getClassLoader();
        Path path = null;
        try {
            String name = clazz.getSimpleName() + ".txt";
            path = Paths.get(classLoader.getResource("datos/" + name).toURI());
        } catch (NullPointerException | URISyntaxException e) {
            logger.error(e);
        }
        try {
            Stream<String> stream = Files.lines(path);
            List<String> stream2 = new ArrayList<>();
            stream.forEach((String s) -> stream2.add(s));
            int count = toIntExact(stream2.size());
            stream2.forEach(s -> initializeArray(count, fieldCount(s)));
            x = 0;
            stream2.forEach(s -> assign(field(s)));
        } catch (IOException e) {
            logger.error(e);
        }
        return result;
    }

我实现新闻适配器的Adapter类

公共类NewsAdapter扩展了RecyclerView.Adapter {

    below my news_items.xml where I have implemented article list



 <?xml version="1.0" encoding="utf-8"?>

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_marginTop="30dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"

        app:cardCornerRadius="6dp"
        app:cardElevation="6dp"
        android:layout_marginBottom="10dp"
        android:layout_height="wrap_content">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/article_Image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="30dp"
                android:scaleType="fitXY"
                android:text="TextView"
                app:layout_constraintBottom_toTopOf="@+id/article_Author"
                app:layout_constraintDimensionRatio="H,1:1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_chainStyle="packed" />

            <TextView
                android:id="@+id/article_Author"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="16dp"
                android:lineSpacingExtra="8dp"
                android:text="TextView"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textColor="#DE000000"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/article_Image" />

            <TextView
                android:id="@+id/article_Title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="20dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="16dp"
                android:lineSpacingExtra="8dp"
                android:text="TextView"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textColor="#DE000000"
                android:textSize="20sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/article_Author"
                app:layout_constraintVertical_bias="0.0" />

            <TextView
                android:id="@+id/article_Date"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:layout_marginEnd="20dp"
                android:layout_marginBottom="20dp"
                android:lineSpacingExtra="8dp"
                android:text="TextView"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textColor="#DE000000"
                android:textSize="14sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/article_Title"
                app:layout_constraintVertical_bias="0.0" />

            <TextView
                android:id="@+id/article_Description"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="45dp"
                android:lineSpacingExtra="8dp"
                android:text="TextView"
                android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                android:textColor="#DE000000"
                android:textSize="14sp"
                android:textStyle="bold"
                android:padding="50dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/article_Date" />




            <ImageButton
                android:id="@+id/copy_Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="32dp"
                android:layout_marginEnd="48dp"
                android:layout_marginRight="48dp"
                android:background="#00FFFFFF"
                android:padding="10dp"
                android:layout_below="@id/article_Description"
                app:layout_constraintBottom_toBottomOf="@+id/share_button"
                app:srcCompat="@drawable/ic_copy" />

            <ImageButton
                android:id="@+id/share_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:background="#00FFFFFF"
                android:padding="20dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:srcCompat="@drawable/ic_share" />
            <!--<LinearLayout-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_gravity="center"-->
            <!--android:orientation="horizontal">-->

            <!--<ImageButton-->
            <!--android:id="@+id/copy_Button"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_below="@id/article_Description"-->
            <!--android:layout_weight="1"-->
            <!--android:background="@drawable/ic_copy"-->
            <!--android:maxLines="1"-->
            <!--android:text="textview1" />-->


            <!--<ImageButton-->
            <!--android:id="@+id/share_button"-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_weight="0"-->
            <!--android:background="@drawable/ic_share"-->
            <!--android:text="textview2" />-->

            <!--</LinearLayout>-->
            -->
        </android.support.constraint.ConstraintLayout>





</android.support.v7.widget.CardView>
    **below my MainActivity where I am using bottom NavigationBar and hosting four news.**


    public class MainActivity extends BottomBarHolderActivity implements AllJazeeraFragment.OnFragmentInteractionListener, BBCFragment.OnFragmentInteractionListener, CNNFragment.OnFragmentInteractionListener, CBCNewsFragment.OnFragmentInteractionListener {

        //    private ApiService apiService;
        public static final String url_key = "urlKey";
        final int Bottom_CAPACITY = 7;
        public ArrayList<Article> articleList = new ArrayList<>();


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

            Article article = new Article(Parcel.obtain());
            article.setUrl(article.getUrl());
            Intent i = new Intent(this, DetailActivity.class);
            i.putExtra("urlKey", article);
    //        // using the (String name, Parcelable value) overload!
    //        startActivity(i);

            NavigationPage page1 = new NavigationPage("All Jazeera", ContextCompat.getDrawable(this, R.drawable.alljazeera), AllJazeeraFragment.newInstance());
            NavigationPage page2 = new NavigationPage("BBC", ContextCompat.getDrawable(this, R.drawable.bbc_icon), CNNFragment.newInstance());
            NavigationPage page3 = new NavigationPage("CNN", ContextCompat.getDrawable(this, R.drawable.cnn_icon), AllJazeeraFragment.newInstance());
            NavigationPage page4 = new NavigationPage("CBC", ContextCompat.getDrawable(this, R.drawable.cbc_icon), CBCNewsFragment.newInstance());
            //NavigationPage page5 = new NavigationPage("Menu", ContextCompat.getDrawable(this, R.drawable.icon_menu), Menu.newInstance());
            List<NavigationPage> navigationPages = new ArrayList<>(Bottom_CAPACITY);
            Boolean navigation = navigationPages.size() > 6;
            navigationPages.add(page1);
            navigationPages.add(page2);
            navigationPages.add(page3);
            navigationPages.add(page4);


            super.setupBottomBarHolderActivity(navigationPages);


        }


        public void onClicked() {
            Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show();
        }

    }

below my AllJazeraFragment other fragment news classes the same

    public class AllJazeeraFragment extends Fragment {


        public NewsAdapter adapter;
        public ArrayList<Article> articleList = new ArrayList();
        RecyclerView recyclerView;

        private AllJazeeraFragment.OnFragmentInteractionListener listener;

        public static AllJazeeraFragment newInstance() {
            return new AllJazeeraFragment();
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


        }

        @Override
        public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
            final View rootView = inflater.inflate(R.layout.alljazeera_fragment, container, false);

            NewsInterface apiService = NewsClient.getApiService();
            Call<ArticleResponse> call = apiService.getAllJazeera();

            call.enqueue(new Callback<ArticleResponse>() {
                @Override
                public void onResponse(Call<ArticleResponse> call, Response<ArticleResponse> response) {

                    articleList = new ArrayList(response.body().getArticles());
                    recyclerView = rootView.findViewById(R.id.recycler_view);
                    adapter = new NewsAdapter(articleList);
                    RecyclerView.LayoutManager eLayoutManager = new LinearLayoutManager(getActivity());
                    recyclerView.setLayoutManager(eLayoutManager);
                    recyclerView.setAdapter(adapter);

                }

                @Override
                public void onFailure(Call<ArticleResponse> call, Throwable t) {

                }
            });

            return rootView;
        }

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            if (context instanceof AllJazeeraFragment.OnFragmentInteractionListener) {
                listener = (AllJazeeraFragment.OnFragmentInteractionListener) context;
            } else {
                throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener");
            }
        }

        @Override
        public void onDetach() {
            super.onDetach();
            listener = null;
        }

        public interface OnFragmentInteractionListener {
        }


    }

    alljazera_fragment.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


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

    </LinearLayout>

请帮助我解决问题,

致谢,Yodgorbek

1 个答案:

答案 0 :(得分:0)

也许这种布局将为您带来您希望获得的结果。这只是CardView,您可能会希望将其嵌入FrameLayout中。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/article_Image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        app:srcCompat="@drawable/automobile"
        />

    <TextView
        android:id="@+id/article_Author"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="TextView"
        android:maxLines="2"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="#DE000000"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_below="@id/article_Image"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        />

    <TextView
        android:id="@+id/article_Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:lineSpacingExtra="8dp"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="#DE000000"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_below="@id/article_Author"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        />

    <TextView
        android:id="@+id/article_Date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="20dp"
        android:lineSpacingExtra="8dp"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="#DE000000"
        android:textSize="14sp"
        android:textStyle="bold"
        android:layout_below="@id/article_Title"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        />

    <TextView
        android:id="@+id/article_Description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/really_long_text_250"
        android:lineSpacingExtra="8dp"
        android:maxLines="3"
        android:textColor="#DE000000"
        android:textSize="14sp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_marginStart="50dp"
        android:layout_marginEnd="50dp"
        android:layout_below="@id/article_Date"
        android:layout_above="@id/buttonContainer"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        />


    <RelativeLayout
        android:id="@+id/buttonContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        >
        <ImageButton
            android:id="@+id/copy_Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00FFFFFF"
            android:layout_alignParentStart="true"
            android:padding="20dp"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            app:srcCompat="@drawable/ic_event_black_24dp" />

        <ImageButton
            android:id="@+id/share_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00FFFFFF"
            android:layout_alignParentEnd="true"
            android:padding="20dp"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            app:srcCompat="@drawable/ic_contacts_black_24dp" />
    </RelativeLayout>
</RelativeLayout>



注意:
我选择RelativeLayout是因为我更熟悉它。


如果您有任何问题或布局不符合需要的设计规范,请告诉我。


不要纠正“半岛电视台”的拼写。