在以编程方式创建的ScrollView

时间:2018-11-24 10:09:14

标签: android android-studio android-layout

我正在创建一个ScrollView,嵌套在RelativeLayout(包含表)中。一切都已正确添加到布局中。问题是:垂直滚动工作正常,但水平滚动根本不工作。我已经测试了MATCH_PARENTWRAP_CONTENTFILL_PARENT的各种组合。没有尝试甚至关闭。所以我的问题是:我在这里想念什么?

  

代码如下:

public class PlayerView extends View {

    private RelativeLayout relativeLayout;
    private TableLayout tableLayout;
    private int player_loop;
    private int player_count;

    public PlayerView(Context context, int player_count){
        super(context);
        setPlayer_count(player_count);
    }

    public ScrollView create_scrollView(){

        ScrollView scrollView = new ScrollView(getContext());
        ScrollView.LayoutParams scroll_params = new ScrollView.LayoutParams(
                ScrollView.LayoutParams.MATCH_PARENT,
                ScrollView.LayoutParams.MATCH_PARENT
        );
        scrollView.setLayoutParams(scroll_params);
        scrollView.addView(create_relativeLayout());

        return scrollView;
    }

    public RelativeLayout create_relativeLayout(){

        relativeLayout = new RelativeLayout(getContext());
        RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT
        );
        relativeLayout.setLayoutParams(relativeParams);
        create_Player_Table_Layout();

        return relativeLayout;
    }

    public void create_Player_Table_Layout(){

        for(player_loop = 1; player_loop <= player_count; player_loop++){

            relativeLayout.addView(player_table(getResources().getString(R.string.dummy_player_name) + player_loop, player_loop));

        }
    }

    public TableLayout player_table(String playername, int playernumber){

        tableLayout = new TableLayout(getContext());
        tableLayout.setId(playernumber * 1000);

        if (playernumber > 1) {

            //TABLE PLACEMENT
            RelativeLayout.LayoutParams tbl_params_New = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            tbl_params_New.addRule(RelativeLayout.RIGHT_OF, (playernumber - 1) * 1000);
            tableLayout.setLayoutParams(tbl_params_New);
        }

        //Add Playername
        TableRow row_playername = new TableRow(getContext());
        TextView view_name = new TextView(getContext());

        TableRow.LayoutParams view_name_params = new TableRow.LayoutParams();
        view_name_params.setMargins(20,20,20,20);

        view_name.setLayoutParams(view_name_params);
        view_name.setGravity(Gravity.CENTER);
        view_name.setText(playername);
        view_name.setTextSize(20);
        view_name.setId(playernumber * 100);

        row_playername.addView(view_name);
        tableLayout.addView(row_playername);

        //Add Lifepoints
        TableRow row_lifepoints = new TableRow(getContext());
        TextView view_lifepoints = new TextView(getContext());

        TableRow.LayoutParams view_lifepoints_params = new TableRow.LayoutParams();
        view_lifepoints_params.setMargins(20, 0, 20, 20);

        view_lifepoints.setText("40");
        view_lifepoints.setTextSize(40);
        view_lifepoints.setGravity(Gravity.CENTER);
        view_lifepoints.setId(playernumber * 100 + 10);
        view_lifepoints.setLayoutParams(view_lifepoints_params);
        row_lifepoints.addView(view_lifepoints);
        tableLayout.addView(row_lifepoints);

        Log.d("Test", "Player count:" + player_count);
        for(int opponent_loop = 1; opponent_loop <= player_count; opponent_loop++){
            tableLayout.addView(commander_damage_from_player(player_loop, opponent_loop));
        }

        return tableLayout;
    }

    private TableRow commander_damage_from_player(int player_loop, int opponent_loop){

        TableRow row = new TableRow(getContext());

        TextView textView = new TextView(getContext());

        TableRow.LayoutParams layoutParams = new TableRow.LayoutParams();
        layoutParams.setMargins(20, 0, 40, 20);
        textView.setLayoutParams(layoutParams);

        textView.setText("0 | " + getResources().getString(R.string.dummy_player_name)+ " " + opponent_loop);
        textView.setTextSize(20);
        textView.setId(player_loop + 100 + opponent_loop);
        row.addView(textView);

        return row;
    }

    private void setPlayer_count(int player_count){
        this.player_count = player_count;
    }

}
  

并记录调用类:

public class Home extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PlayerView playerView = new PlayerView(this, 8);
        setContentView(playerView.create_scrollView());

    }
}

1 个答案:

答案 0 :(得分:1)

唯一的方法是创建一个horizo​​ntalscrollview作为scrollview的子级。如果您对create_relativeLayout()进行了较小的修改以创建水平滚动视图,那么它将可以正常工作。我已经在其他示例中看到了这种情况。

public  HorizontalScrollView create_relativeLayout(){

    HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());

    relativeLayout = new RelativeLayout(getContext());
    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT
    );
    relativeLayout.setLayoutParams(relativeParams);
    create_Player_Table_Layout();
    horizontalScrollView.addView(relativeLayout);
    return horizontalScrollView;
}