简体   繁体   English

添加项目以在 Android 中以编程方式查看

[英]Adding item to view programmatically in Android

I am trying to find out how I can add an item (from xml drawable/workaround_item.xml) in a LinearLayout that is vertically aligned.我试图找出如何在垂直对齐的 LinearLayout 中添加项目(来自 xml drawable/workaround_item.xml)。

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="100dip"
  android:layout_marginTop="5dip"
  android:paddingTop="5dip"
  android:paddingBottom="5dip"
  android:orientation="horizontal">

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_alignParentLeft="true"
        android:gravity="center_vertical">            
        <ImageView 
            android:id="@+id/home_image"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" />           
        <TextView
            android:id="@+id/home_team" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" 
            android:gravity="center_horizontal"
            android:textStyle="bold"
            android:textSize="12sp"
            android:textColor="#fff"/>          
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center_vertical"
        android:layout_centerHorizontal="true">
        <TextView 
            android:id="@+id/game_cship" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" 
            android:gravity="center_horizontal|center_vertical"
            android:textStyle="bold"
            android:textSize="10sp"/>               
        <TextView 
            android:id="@+id/game_score" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" 
            android:gravity="center_horizontal"
            android:textStyle="bold"
            android:textColor="#fff" android:textSize="24sp"/>          
        <TextView 
            android:id="@+id/game_date" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" 
            android:gravity="center_horizontal"
            android:textStyle="bold"
            android:textSize="10sp"/>           
    </LinearLayout>

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center_vertical"
        android:layout_alignParentRight="true">                       
        <ImageView 
            android:id="@+id/away_image"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" />           
        <TextView 
            android:id="@+id/away_team" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:textStyle="bold"
            android:textSize="12sp"
            android:textColor="#fff" />     
    </LinearLayout>
</RelativeLayout>

At the beginning I was trying to use a ListView but as far I as know there is a bug using listview inside a scrollview.一开始我试图使用 ListView 但据我所知在滚动视图中使用 listview 存在一个错误。 I was doing this because I have more components on the view that should be scrolled together with the list view.我这样做是因为视图上有更多组件应该与列表视图一起滚动。 So I decided to remove the listview and implement a simple linear layout where i will be adding each element below other and so on.所以我决定删除列表视图并实现一个简单的线性布局,我将在其他元素下面添加每个元素等等。

The layout for the activity is as follow (layout/workaround.xml) and I would like to start adding the elements below the top bar.活动的布局如下(layout/workaround.xml),我想开始在顶部栏下方添加元素。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"    
android:id="@+id/linearid" >

</LinearLayout>

That's my Java code:这是我的Java代码:

public class DemoActivity3 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workaround);

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearid);

    for(int i = 1; i< 15; i++) {        
        View item = inflater.inflate(R.layout.workaround_item, null);
        TextView x = (TextView) item.findViewById(R.id.game_score);
        x.setText("Score" + i);
        TextView ht = (TextView) item.findViewById(R.id.home_team);
        ht.setText("HomeTeam" + i);
        TextView at = (TextView) item.findViewById(R.id.away_team);
        at.setText("AwayTeam"+i);
        ImageView hi = (ImageView) item.findViewById(R.id.home_image);
        hi.setImageResource(R.drawable.gremio);
        ImageView ai = (ImageView) item.findViewById(R.id.away_image);
        ai.setImageResource(R.drawable.goias);
        ll.addView(item,ViewGroup.LayoutParams.WRAP_CONTENT);
    }
}

That's the result I am getting: http://twitpic.com/3562yu这就是我得到的结果: http : //twitpic.com/3562yu

Does Android allow us to do this? Android 允许我们这样做吗? If so, Hoe can I add the elements and set their values?如果是这样,我可以添加元素并设置它们的值吗? (this layout will be displaying results of soccer games). (此布局将显示足球比赛的结果)。

You can use the LayoutInflater to create an instance of an xml layout file.您可以使用LayoutInflater创建一个 xml 布局文件的实例。

Then just call addView on your LinearLayout.然后只需在您的 LinearLayout 上调用addView

There's no "bug" about ListView in ScrollView, this is just nonsense. ScrollView 中的 ListView 没有“错误”,这只是废话。

There's a feature in ListView that allow you to add header / footer views which would allow you to place anything above and below the regular items of your ListView. ListView 中有一项功能允许您添加页眉/ 页脚视图,这将允许您在 ListView 的常规项目的上方和下方放置任何内容。

As Kevin said you're better to use a list view with a header.正如凯文所说,您最好使用带有标题的列表视图。 The header will scroll with the list.标题将随列表滚动。

However if you're programatically adding views to a layout, you're code should work.但是,如果您以编程方式向布局添加视图,则代码应该可以工作。 Try adding them with a LayoutParam object:尝试使用 LayoutParam 对象添加它们:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ll.addView(item, params);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android-将项目添加到滚动视图并以编程方式滚动 - Android - Adding an item to a scroll view and scroll programmatically 以编程方式将视图添加为菜单项 - Programmatically adding a view as a menu item Android:以编程方式将视图添加到RelativeLayout - Android: programmatically adding view to RelativeLayout Android以编程方式将带有LinearLayout的自定义视图添加到ListView - Adding Custom View With LinearLayout to ListView Programmatically Android Android以编程方式添加视图而不监听属性 - Android adding view programmatically not listening to attributes Android以编程方式向AlertDialog中的ArrayAdapter视图添加边框 - Android programmatically adding border to ArrayAdapter view in AlertDialog 以编程方式向 RecyclerView 项添加一些视图会创建一个已知错误 - Adding Some View to RecyclerView item programmatically creates an known bug 以编程方式将项目添加到CardView - Programmatically adding an item to a CardView 以编程方式将子视图添加到Android中的自定义视图 - Adding a child view to a custom view within Android, programmatically Android性能:以编程方式添加视图,将视图设置为GONE / VISIBLE - Android Performance : Adding view programmatically vs setting view to GONE/VISIBLE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM