I am new in the Android programming I need to know How do I add two columns to a listview like this.
What is called? Do I need to modify the XML file only or will I need to modify the Java file too? This is my Code!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal"
android:layout_weight="7"
android:padding="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:padding="4dp">
<ImageView
android:id="@+id/galleryImage"
android:layout_width="70dp"
android:layout_height="50dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="7"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shadowColor="#000"
android:shadowRadius="1.0"
android:textColor="#666666"
android:textSize="17sp"
android:layout_marginBottom="5dp"
android:maxLines="2"
android:scrollHorizontally="true"
android:ellipsize="end"
android:text="This is news title"/>
<TextView
android:id="@+id/sdetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#777777"
android:textSize="16sp"
android:maxLines="2"
android:minLines="2"
android:scrollHorizontally="true"
android:ellipsize="end"
android:text="This is news description bla bla bla..."/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/author"
android:textSize="15sp"
android:textColor="#42A5F5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="News Author"/>
</LinearLayout>
<TextView
android:id="@+id/time"
android:gravity="right"
android:textSize="14sp"
android:textColor="#42A5F5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
you can try using below code for xml changes and Use the Gridview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical"
android:padding="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:padding="4dp">
<ImageView
android:id="@+id/galleryImage"
android:layout_width="70dp"
android:layout_height="50dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shadowColor="#000"
android:shadowRadius="1.0"
android:textColor="#666666"
android:textSize="17sp"
android:layout_marginBottom="5dp"
android:maxLines="2"
android:scrollHorizontally="true"
android:ellipsize="end"
android:text="This is news title"/>
<TextView
android:id="@+id/sdetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#777777"
android:textSize="16sp"
android:maxLines="2"
android:minLines="2"
android:scrollHorizontally="true"
android:ellipsize="end"
android:text="This is news description bla bla bla..."/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/author"
android:textSize="15sp"
android:textColor="#42A5F5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="News Author"/>
</LinearLayout>
<TextView
android:id="@+id/time"
android:gravity="right"
android:textSize="14sp"
android:textColor="#42A5F5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
you can also use recycler view to show your layout in two columns using Recyclerview set with layout manager as -
public class YourClass extends AppCompatActivity {
RecyclerView rv;
RecyclerView.LayoutManager lm;
// Create custom adapter MyRecyclerView extends to RecyclerView.Adapter
implements all methods and bind the data inside OnBindView() method on
custom design
MyRecyclerView adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_activity);
rv =(RecyclerView) findViewById(R.id.rv_id);
rv .setHasFixedSize(true)
lm = new GridLayoutManager(context, 2);
rv .setLayoutManager(lm);
// get the data of content to be bind on recyclerview with adapter
adapter = new MyRecyclerView(context, data);
rv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
Use Row Layout and gridview for better result
you can follow the step from following website,it is a best, https://www.tutorialspoint.com/android/android_grid_view.htm
您可以通过使用Recyclerview来解决此问题,在该视图中,其LayoutManager设置为网格布局管理器。
For that you have to set your LayoutManager as grid layout manager.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.