0

我想以编程方式保持重力来更改我的视图(CanvasView 扩展视图)的大小。我的视图在RelativeLayout 里面。我尝试了许多不同的方法,但我被卡住了......

关键是,我想根据屏幕尺寸设置尺寸:

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

        ToolsAndConstants.metrics = new DisplayMetrics();

        windowManager =  (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(ToolsAndConstants.metrics);

        setContentView(R.layout.activity_dictionary);

        CanvasView canvasView = (CanvasView) findViewById(R.id.activity_dictionary_CanvasView);
        canvasView.canvasWidth  = (ToolsAndConstants.metrics.widthPixels / 10 * 7) - 10;
        canvasView.canvasHeight = (ToolsAndConstants.metrics.heightPixels / 10 * 4) - 10; 
        canvasView.setLayoutParams(new RelativeLayout.LayoutParams(canvasView.canvasWidth, canvasView.canvasHeight));

    (...)
    }

XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_dictionary_relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gray_patterns_vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".LogReader" >

    <LinearLayout
        android:id="@+id/activity_dictionary_linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="vertical" >

        <ProgressBar
            android:id="@+id/activity_dictionary_progressBar1"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:id="@+id/activity_dictionary_textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Loading dictionary..."
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@android:color/white" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/activity_dictionary_frameLayout1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" >

        <view
            android:id="@+id/activity_dictionary_CanvasView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="0dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            class="com.example.dictionary.CanvasView"
            android:orientation="vertical" />
    </FrameLayout>

</RelativeLayout>

我该如何调整它的大小?

4

1 回答 1

2

我找到了解决方案:

canvasView = (CanvasView) findViewById(R.id.activity_dictionary_CanvasView);
canvasView.canvasWidth  = 100;
canvasView.canvasHeight = 100;
RelativeLayout.LayoutParams layoutParams = 
         new RelativeLayout.LayoutParams(canvasView.canvasWidth, canvasView.canvasHeight);
//I want my Canvas be above LinearLayout and Horizontaly centered
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layoutParams.addRule(RelativeLayout.ABOVE, R.id.activity_dictionary_linearLayout2);
canvasView.setLayoutParams(layoutParams);
于 2013-06-10T06:47:19.380 回答