Android 以编程方式创建linearlayout

Android 以编程方式创建linearlayout,android,android-linearlayout,Android,Android Linearlayout,我想创建一个新闻版面,并通过数据库更新它的内容。因为我并没有关于新闻数量的任何信息,所以我必须使用For循环来创建合适的模板。我设计了我的样本模板。现在我想知道,我如何用线性布局实用地创建它 创建一个带边框的_green_background.xml并将其放入其中 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"

我想创建一个新闻版面,并通过数据库更新它的内容。因为我并没有关于新闻数量的任何信息,所以我必须使用For循环来创建合适的模板。我设计了我的样本模板。现在我想知道,我如何用线性布局实用地创建它


创建一个带边框的_green_background.xml并将其放入其中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="#A8CF8B"/>
    <stroke
        android:width="1dp"
        android:color="#051014"/>

</shape>
现在用它做点什么,但是诚实地使用ListView或RecyclerView。。。这是效率较低的方式

注意。。。我在边境上失败了,但这不是问题…:)


您不必通过编程来实现,您可以使用ListView或RecyclerView来实现这一点。如果您的数据库中有很多条目,那么它的效率也会更高谢谢您的回复,但是我的项目中有一些错误:错误:找不到与给定名称匹配的资源(在'background'处,值为'@drawable/bordered_background_blue')。千万不要盲目复制/粘贴其他人的代码。现在检查您的drawables文件名。您必须创建它。我已经把它的来源给它了。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="#9CC2E6"/>
    <stroke
        android:width="1dp"
        android:color="#4C5058"/>

</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="#8EA9DC"/>
    <stroke
        android:width="1dp"
        android:color="#051014"/>
</shape>
<TextView
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_weight="0.2"
    android:paddingTop="5dp"
    android:paddingLeft="5dp"
    android:background="@drawable/bordered_background_blue"
    android:text="Date"/>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_weight="0.75"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@drawable/bordered_background_blue_2"
        android:gravity="center_vertical"
        android:id="@+id/tvTopic"
        android:text="topic"
        android:paddingLeft="10dp"
        android:layout_weight="0.5"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@drawable/bordered_green_background"
        android:id="@+id/tvContent"
        android:text="content"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:layout_weight="0.5"/>

</LinearLayout>
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.demo_linear_layout, null, false);
TextView tvContent = (TextView) layout.findViewById(R.id.tvContent);