0

I have a customm view which generates an arc and i've put in the xml like this

<com.example.application.ArcView
        android:id="@+id/arcView1"
        android:layout_width="@string/arc_size"
        android:layout_height="@string/arc_size"
        android:layout_centerInParent="true" />

I have the height and width that change based on the screen density

I've coded it like this but it doesn't work

Resources res = getResources();
    String arcSizeString = res.getString(R.string.arc_size);

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    switch(metrics.densityDpi){
         case DisplayMetrics.DENSITY_LOW:
             arcSize = 170;
             arcSizeString = (arcSize + "px");
                    break;
         case DisplayMetrics.DENSITY_MEDIUM:
             arcSize = 227;
             arcSizeString = (arcSize + "px");
                     break;
         case DisplayMetrics.DENSITY_HIGH:
             arcSize = 342;
             arcSizeString = (arcSize + "px");
                     break;
    }       

How can i set the width and height to a variable?

4

1 回答 1

2

可能,您想问“如何将宽度和高度设置为视图”。

您需要在 Activity 类中引用 ArcView 的实例,更准确地说是在 Activity 的 onCreate 方法中,如下所示:

ArcView arcView = (ArcView)findViewById(R.id.arcView1);

之后,根据密度计算宽度和高度,并将其设置为您的视图

arcView.setLayoutParams(myLayoutParams);

线性布局.LayoutParams

于 2012-08-19T15:14:46.640 回答