简体   繁体   English

如何在Android中以编程方式将TextView设置为LinearLayout?

[英]How can I set TextView into LinearLayout programmatically in Android?

I am creating a TextView dynamically. 我正在动态创建一个TextView Here, I am creating a mainLayout in which I have two child layout and I wan gave them weight. 在这里,我正在创建一个mainLayout,其中有两个子布局,并希望为其赋予权重。

Here, is my Layout 这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:baselineAligned="false"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/Layout_second_overs"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="6"
            android:orientation="vertical">

            <!--Second textview overs-->
        </LinearLayout>
        <LinearLayout
            android:id="@+id/Layout_second_balls"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <!--Second textview balls--> //here i inflate 12 textview dynamically but not 12 seen on the device while i run the app.
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Code to create a 12 TextViews and put into layout. 创建12个TextViews并放入布局的代码。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

for(int a = 0; a <= 12; a++)
{
    TextView first = new TextView(getApplicationContext());
    first.setLayoutParams(params);
    first.setTextColor(Color.parseColor("#000000"));
    first.setTextSize(12);
    first.setGravity(Gravity.LEFT);
    first.setPadding(5, 0, 5, 0);
    first.setText("12");
    layout_second_balls.addView(first); //This is my linear layout which id is Layout_second_balls
}

Problem is my all data are print into log but not visible on the device. 问题是我的所有数据都已打印到日志中,但在设备上不可见。

Please, help me to solve out this problem. 请帮我解决这个问题。

You have to assign index for a view while performing addview(view,index) set index for each textview created in for loop like layout_second_balls.addView(first,index); 您必须为在for循环中创建的每个textview执行addview(view,index)设置索引addview(view,index)同时为视图分配索引,例如layout_second_balls.addView(first,index);

Code should look like : 代码应如下所示:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for(int a = 0; a <= 12; a++)
{
    TextView first = new TextView(getApplicationContext());
    first.setLayoutParams(params);
    first.setTextColor(Color.parseColor("#000000"));
    first.setTextSize(12);
    first.setGravity(Gravity.LEFT);
    first.setPadding(5, 0, 5, 0);
    first.setText("12");
    layout_second_balls.addView(first,a);
}

Hope it will work. 希望它能工作。

You can use this line of code. 您可以使用此行代码。

LinearLayout  layout_second_balls=(LinearLayout) findViewById(R.id.Layout_second_balls);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    for(int a = 0;a<=12;a++)
    {
        TextView first = new TextView(getApplicationContext());
        first.setLayoutParams(params);
        first.setTextColor(Color.parseColor("#000000"));
        first.setTextSize(12);
        first.setGravity(Gravity.LEFT);
        first.setPadding(5, 0, 5, 0);
        first.setText("12");
        layout_second_balls.addView(first);
    }

在此处输入图片说明

i run this code in my emulator and i got this out put. 我在模拟器中运行此代码,然后将其放出。

Your layout_second_overs requires all of your screen space - so your layout_second_balls is moved out of your screen. 您的layout_second_overs了您所有的屏幕空间-因此您的layout_second_balls已移出屏幕。 If you want to see your two LinearLayouts beside each other, you need to set the layout_width to "0dp": 如果要彼此并排查看两个LinearLayouts ,则需要将layout_width设置为“ 0dp”:

//pseudo, simply change width to 0dp

<LinearLayout
    android:layout_height="40dp"
    .... >
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="6"/>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"/>

</LinearLayout>

To replace this kind of loops you are supposed to use a RecyclerView . 要替换这种循环,您应该使用RecyclerView It is easy to use and responsive (since your list will be scrollable). 它易于使用和响应(因为您的列表将是可滚动的)。 The things you have to remember while implementing a RecyclerView are to add an adapter and a LayoutManager to it. 在实现RecyclerView时必须记住的事情是向其添加适配器和LayoutManager。

For your example : 例如:

LayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true);
YourAdapter adapter = new YourAdapter([...]);
RecyclerView recyclerView = findViewById(R.id.your_recyclerview_here);

recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);

If you need any advices for creating your adapter just ask. 如果您需要任何有关创建适配器的建议,请询问。

There are two issues 有两个问题

  • One is for the Layout Design related 一是与布局设计有关的
  • Second based on the layout design and property you also need to handle is while you are adding dynamically view into linear layout 其次,根据布局设计和属性,您还需要处理将动态视图添加到线性布局中的过程

Here what i have changed in Your xml file 这是我在您的xml文件中所做的更改

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:baselineAligned="false"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/Layout_second_overs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!--Second textview overs-->
        </LinearLayout>
        <LinearLayout
            android:id="@+id/Layout_second_balls"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1" android:background="#FF13C7FF"
            android:orientation="horizontal">

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

In Layout_second_overs linearlayout height , Width & weight is the issue. Layout_second_overs linearlayout height中,宽度和重量是问题。
& in Layout_second_balls linear layout you have used weight as 1 so you also required layout_height as 0dp if orientation is horizontal &在Layout_second_balls线性布局中,您已将weight设为1因此,如果方向是水平的,还需要将layout_height设为0dp

Now below things i have changed in code 现在下面的事情我已经改变了代码

LinearLayout.LayoutParams params = new
                LinearLayout.LayoutParams
                (0, LinearLayout.LayoutParams.WRAP_CONTENT);
        for(int a = 0;a<=12;a++)
        {
            TextView first = new TextView(getActivity());
            first.setLayoutParams(params);
            first.setTextColor(Color.parseColor("#000000"));
            first.setTextSize(12);
            params.weight = 1; /// Need to give weight if you want equal size of textview inside Linear
            first.setGravity(Gravity.LEFT);
            first.setPadding(5, 0, 5, 0);
            first.setText("12");
            Layout_second_balls.addView(first); //This is my linear layout which id is Layout_second_balls
        }

above things are based on the question which you ask 以上是根据您提出的问题

Suggestion 建议

Avoid adding dynamic view like you asked . 避免像您要求的那样添加动态视图。 prefer to use ListView, GridView, RecyclerView for this type of functionality. 倾向于将ListView,GridView,RecyclerView用于此类功能。

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

相关问题 如何在Android中以编程方式将样式和主题设置为linearlayout? - How can I set Styles and Themes to linearlayout, programmatically in android? 如何以编程方式在android中的linearlayout中给textview位置 - how to give positions to textview in linearlayout in android programmatically 如何在LinearLayout的同一行上设置Imageview和Textview - How can I set Imageview and Textview on same line in LinearLayout 如何在LinearLayout中正确设置多行TextView省略号 - How can I set multiline TextView Ellipsis properly in LinearLayout 如何在LinearLayout中设置TextView的文本? - How can I set the text of the TextView within a LinearLayout? 我如何制作动态线性布局以在 android 中显示文本视图? - how can i make dynamic linearlayout to show textview in android? 如何在Android中以编程方式将LinearLayout设置为另一个LinearLayout? - How can i set LinearLayout into another LinearLayout programatically in android? Android - 如何在 Linearlayout 中以编程方式设置按钮样式? - Android - How to programmatically set the button style in a Linearlayout? Android:如何以编程方式设置LinearLayout marging - Android: How to set LinearLayout marging programmatically 如何在中心LinearLayout上找到TextView? - How can I locate TextView on center LinearLayout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM