小编bil*_*l23的帖子

动态调整大小片段android

我想改变我在MaiActivity中使用的片段的宽度?但我发现它无法LayoutParams更改其默认宽度:

public class MainActivity extends Activity {

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        int widthFfragment = 300;

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        widthTot = size.x;
        heightTot = size.y;

        findViewById(R.id.f1).getLayoutParams().width = widthTot-widthFfragment;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的片段代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/f1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#330000"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Fragment1.java

public class Fragment1 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater …
Run Code Online (Sandbox Code Playgroud)

android android-fragments

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

android ×1

android-fragments ×1