Jelajahi Sumber

Формирование новостей

Your Name 3 tahun lalu
induk
melakukan
6bde782887

+ 44 - 7
app/src/main/java/com/example/mytest/MainActivity.java

@@ -3,13 +3,16 @@ package com.example.mytest;
 import android.app.Activity;
 import android.graphics.Color;
 import android.os.Bundle;
+import android.text.TextUtils;
 import android.view.View;
+import android.view.ViewGroup;
 import android.widget.Button;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
 import org.json.JSONArray;
 import org.json.JSONObject;
+import org.w3c.dom.Text;
 
 public class MainActivity extends Activity implements Interface {
 
@@ -25,18 +28,52 @@ public class MainActivity extends Activity implements Interface {
                 LinearLayout news = findViewById(R.id.newContainer);
 
                 try {
-                    for (int i = 0; i < array.length(); i++) {
+                    for (int i = array.length()-1; i >= 0; i--) {
 
                         JSONObject o = array.getJSONObject(i);
 
+                        // Контейнер под 1 новость
+                        LinearLayout newsContainer = new LinearLayout(getApplicationContext());
+                        LinearLayout.LayoutParams newsContainerLayout =
+                                new LinearLayout.LayoutParams(
+                                        LinearLayout.LayoutParams.MATCH_PARENT,
+                                        LinearLayout.LayoutParams.WRAP_CONTENT);
+                        newsContainerLayout.bottomMargin = 20;
+                        newsContainer.setLayoutParams(newsContainerLayout);
+                        newsContainer.setBackgroundColor(Color.WHITE);
+                        newsContainer.setOrientation(LinearLayout.VERTICAL);
+
+                        // Заголовок новости
                         TextView caption = new TextView(getApplicationContext());
+                        LinearLayout.LayoutParams captionLayout =
+                                new LinearLayout.LayoutParams(
+                                        LinearLayout.LayoutParams.MATCH_PARENT,
+                                        LinearLayout.LayoutParams.WRAP_CONTENT);
+                        captionLayout.setMargins(20, 20, 20, 20);
+                        caption.setLayoutParams(captionLayout);
+                        caption.setTextSize(20);
+                        caption.setText(o.getString("Caption"));
+                        caption.setPadding(0, 20, 0, 20);
+                        caption.setTextColor(Color.rgb(50,50,50));
+                        newsContainer.addView(caption);
+
+                        // Текст новости
+                        TextView preview = new TextView(getApplicationContext());
+                        LinearLayout.LayoutParams previewLayout =
+                                new LinearLayout.LayoutParams(
+                                        LinearLayout.LayoutParams.MATCH_PARENT,
+                                        LinearLayout.LayoutParams.WRAP_CONTENT
+                                );
+                        previewLayout.setMargins(20, 50, 20, 20);
+                        preview.setLayoutParams(previewLayout);
+                        preview.setText(o.getString("Preview"));
+                        preview.setTextColor(Color.BLACK);
+                        preview.setMaxLines(6);
+                        preview.setEllipsize(TextUtils.TruncateAt.END);
+                        newsContainer.addView(preview);
+
+                        news.addView(newsContainer);
 
-                        if (o.has("Caption")) {
-                            caption.setText(o.getString("Caption"));
-                            caption.setPadding(0, 20, 0, 20);
-                            caption.setTextColor(Color.RED);
-                            news.addView(caption);
-                        }
                     }
                 } catch (Exception e) {
                     e.printStackTrace();

+ 15 - 7
app/src/main/res/layout/activity_main.xml

@@ -4,29 +4,37 @@
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
+    android:background="#232323"
     tools:context=".MainActivity">
 
     <Button
         android:id="@+id/button"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:layout_marginTop="28dp"
+        android:layout_marginTop="4dp"
         android:text="Button"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintHorizontal_bias="0.498"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
 
-    <LinearLayout
-        android:id="@+id/newContainer"
-        android:layout_width="409dp"
-        android:layout_height="536dp"
+    <ScrollView
+        android:layout_width="0dp"
+        android:layout_height="0dp"
         android:layout_marginStart="1dp"
+        android:layout_marginTop="1dp"
         android:layout_marginEnd="1dp"
-        android:orientation="vertical"
+        android:layout_marginBottom="1dp"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/button"></LinearLayout>
+        app:layout_constraintTop_toBottomOf="@+id/button">
+
+        <LinearLayout
+            android:id="@+id/newContainer"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:orientation="vertical" />
+    </ScrollView>
 
 </androidx.constraintlayout.widget.ConstraintLayout>