简体   繁体   English

如何以编程方式创建TextView,ImageView和相对布局

[英]How to create TextView,ImageView and Relative Layout Programmatically

I am new born in android. 我刚出生在android。 I created a xml file but now i want to create all my layout programmatically. 我创建了一个xml文件,但现在我想以编程方式创建我的所有布局。 I see many examples but i couldn't find my solution. 我看到很多例子,但我找不到我的解决方案。

I want below these component programmatically same :- 我想在编程相同的下面这些组件: -

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

        <ImageView
            android:paddingTop="10dp"
            android:paddingLeft="60dp"
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/bag" />


        <TextView
            android:paddingTop="10dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:textColor="@android:color/black"
            android:layout_centerVertical="true"
            android:layout_gravity="center_horizontal"
            android:layout_toEndOf="@+id/imageView1"
            android:layout_toRightOf="@+id/imageView1"
            android:gravity="center_vertical"
            android:onClick="bugAndLuggage"
            android:text="@string/bug_luggage"
            android:paddingLeft="10dp"/>


    </RelativeLayout>
RelativeLayout relativeLayout=new RelativeLayout(context);
//set Relative Layout Parameter using 
RelativeLayout.LayoutParams relativeLayoutParams=new RelativeLayout.LayoutParams(RelativeLayout.MATCH_PARENT,RelativeLayout.MATCH_PARENT);
relativeLayout.setLayoutParams(relativeLayoutParams);

//Create ImageView in the same manner //以相同的方式创建ImageView

ImageView imageView=new ImageView(context);
relativeLayout.addView(imageView);

// add layout parameters to each view with different options as you //have set it in the xml //在每个视图中添加布局参数,并在xml中设置不同的选项

TextView textView=new TextView(context);
relativeLayout.addView(textView);

UPDATE https://stackoverflow.com/a/21259802/2793134 更新 https://stackoverflow.com/a/21259802/2793134

instead of creating Dynamic views you can use layout inflater to inflate your view. 您可以使用布局inflater来扩展视图,而不是创建动态视图。 so you can use the same xml layout file for inflating the views. 所以你可以使用相同的xml布局文件来扩展视图。

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

    <ImageView
        android:paddingTop="10dp"
        android:paddingLeft="60dp"
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bag" />


    <TextView
        android:paddingTop="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:textColor="@android:color/black"
        android:layout_centerVertical="true"
        android:layout_gravity="center_horizontal"
        android:layout_toEndOf="@+id/imageView1"
        android:layout_toRightOf="@+id/imageView1"
        android:gravity="center_vertical"
        android:onClick="bugAndLuggage"
        android:text="@string/bug_luggage"
        android:paddingLeft="10dp"/>


</RelativeLayout>

and in code 并在代码中

   View v = getLayoutInflater().inflate(R.layout.YOUR_LAYOUT_ID, null);
   RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.srelative_layout_id);
   TextView tv= (TextView)v.findViewBytId(R.id.id_of_text_view); 

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

相关问题 如何创建包含 Imageview 和 textview 的圆形布局? - How to create a rounded layout containing Imageview and a textview? 如何在相对布局中将 Textview 放置在 Imageview 的右侧和中心 - How to place a Textview to the right and center of the Imageview in Relative Layout 将我的textView放在imageView相对布局的中心 - Centering my textView inside imageView relative layout 防止TextView和ImageView在相对布局中重叠 - Preventing TextView and ImageView Overlap In Relative Layout Android-相对布局ImageView中的中心Textview - Android - Center Textview in ImageView of Relative Layout Android相对布局以编程方式更改TextView - Android Relative Layout Change TextView Programmatically 无编程地以编程方式创建ImageView - Create ImageView programmatically without Layout 如何在相对布局中调整TextView和ImageView的大小并将其居中对齐-Android SDK 10 - How to resize and align to center TextView to ImageView in relative layout - android sdk 10 通过触摸移动后,在相对布局中更新 textview/imageview 的位置 - Update the position of textview/imageview at relative layout after moving it by touch 以编程方式在相对布局中创建线性布局 - Create Linear Layout in Relative Layout Programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM