Android - 如何在代码中创建TextView fill_parent

时间:2013-07-06 16:38:57

标签: java android fill-parent

我正在尝试使我的TextView layout_width填充父抛出代码。 这是我的代码:

 TextView WatchMovieTitle;

 private void PrintTheMovieAndYear() {
    if (CountWords() > 23) {
// this is what im doing wrong
 //             WatchMovieTitle.setLayoutParams
 //    (new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        WatchMovieTitle.setText(movieList.get("movieName") + " " + "("
                + movieList.get("movieYear") + ")");
        WatchMovieYear.setVisibility(View.GONE);
    } else {
        WatchMovieYear
                .setText(" " + "(" + movieList.get("movieYear") + ")");

    }
}

谢谢!

3 个答案:

答案 0 :(得分:0)

尝试:

 LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
 WatchMovieTitle.setLayoutParams(Params1);

答案 1 :(得分:0)

您可以使用

LayoutParams lp=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

基于textview的父布局。

答案 2 :(得分:0)

你可以这样做 假设您已将TextView称为电视

tv.setLayoutParams(new ViewGroup.LayoutParams(
        ViewGroup.LayoutParams.FILL_PARENT,
        ViewGroup.LayoutParams.FILL_PARENT));