[英]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 有两个问题
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.