Android HorizontalScrollView中的表布局SetStretCharlColumns(true)在棒棒糖中不起作用

Android HorizontalScrollView中的表布局SetStretCharlColumns(true)在棒棒糖中不起作用,android,Android,我有一个表布局,我把它放在一个水平滚动视图中,因为每行表中的edittext的数量是动态的,所以我用编程的方式把它们放进去。以下代码在版本9(Pie)(也可能在Oreo Go中)中工作得非常好,但在棒棒糖中不起作用。xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http

我有一个表布局,我把它放在一个水平滚动视图中,因为每行表中的edittext的数量是动态的,所以我用编程的方式把它们放进去。以下代码在版本9(Pie)(也可能在Oreo Go中)中工作得非常好,但在棒棒糖中不起作用。xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/activity_main"
tools:context=".MainActivity">

<EditText
    android:id="@+id/mrinp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:hint="Number"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="submit"
    android:onClick="generate"
    />
<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<TableLayout
    android:id="@+id/tbl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:stretchColumns="*"
    android:layout_marginBottom="20dp" />
</HorizontalScrollView>

</LinearLayout>

主要活动

public class MainActivity extends AppCompatActivity {
EditText edr;
TableLayout tl;
LinearLayout myLayout;
int M;
int layoutWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edr = findViewById(R.id.mrinp);
    tl = findViewById(R.id.tbl);
    tl.setStretchAllColumns(true);

    tl.setBackgroundColor(getResources().getColor(R.color._light_green));

}

public void generate(View view) {
    tl.removeAllViews();

    M = Integer.parseInt(edr.getText().toString());
    int setWidth;


    for (int i = 0; i < M; i++) {
        TableRow tr = new TableRow(this);
        tr.setBackgroundColor(getResources().getColor(R.color.cold));

        for (int j = 0; j < M; j++) {

            if(i==0){
                tl.setColumnStretchable(j, true);
                }

            EditText ed = new EditText(this);
            ed.setMinWidth(70);
            tr.addView(ed, j);
        }

        tl.addView(tr);
    }

}

}
public类MainActivity扩展了AppCompatActivity{
编辑文本edr;
表格布局tl;
线性布局我的布局;
int M;
内部布局宽度;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edr=findViewById(R.id.mrinp);
tl=findViewById(R.id.tbl);
tl.SetStretCharl列(真);
tl.setBackgroundColor(getResources().getColor(R.color._浅_绿));
}
公共void生成(视图){
tl.删除所有视图();
M=Integer.parseInt(edr.getText().toString());
int设置宽度;
for(int i=0;i
请注意,
android:stretchColumns=“*”
tl.setStretchAllColumns(true)中的任何一个
tl.setColumnStretchable(j,true)在饼图中按需要工作。这是快照

但是上面的代码在棒棒糖中不起作用(我提到的三个代码都不起作用)。 这是快照

请注意,如果我用棒棒糖中的LinearLayout替换HorizontalScrollView,效果很好

有什么建议吗?

根据

添加android:fillViewport=“true”



如果解决方案对您有效,那么接受答案,这样可能会对其他人有所帮助。我对您的答案投了更高的票,正如您前面提到的,这看起来确实像是Yugandhar Babu的答案的副本,我也对其投了更高的票。
<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     android:fillViewport="true">
<TableLayout
    android:id="@+id/tbl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:stretchColumns="*"
    android:layout_marginBottom="20dp" />
</HorizontalScrollView>