【问题标题】:is it possible to set the imageview heights 1/3 of screen size [duplicate]是否可以将图像视图高度设置为屏幕尺寸的 1/3 [重复]
【发布时间】:2014-05-21 11:47:42
【问题描述】:

请考虑给定代码的代码。 如何使这些图像高度为屏幕尺寸的 1/3。如果我将它与适配器一起使用。 请参考图片

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
           android:paddingTop="0dip" android:layout_gravity="top"
         >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"    
            android:paddingBottom="5dp"
            android:paddingLeft="10dp"
            android:paddingTop="5dp" >

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_gravity="left|top"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher" />
            <ImageView
                android:id="@+id/imaget"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left|top"
                android:layout_weight="1"
                android:scaleType="centerCrop"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>

</TableLayout>

【问题讨论】:

    标签: android xml


    【解决方案1】:

    您可以获取屏幕的宽度和高度,并为两者设置一个参数 按钮的 layout_width 和 layout_height。 怎么样??

    Display display=((WindowManager)getSystemService(Context.WINDOW_SERVICE)).
                                          getDefaultDisplay();
        int width=display.getWidth();
        int height=display.getHeight();
    

    //计算宽度和高度

    说屏幕240*320

    buttonWidth=width/5;     result: buttonWidht=48;
     buttonHeight=height/10   result: buttonHeight=32;
    

    使用 LayoutParams 对象,设置按钮的宽度和高度。

    现在,如果您在屏幕尺寸为 320*480 的设备上运行此代码

     buttonWidth=width/5;     result: buttonWidht=64;
     buttonHeight=height/10   result: buttonHeight=48;
    

    其他设备也是如此。

    【讨论】:

    • 显示“LazyImageLoadAdapter 类型的方法 getSystemService(String) 未定义”
    • 你必须通过你的活动上下文
    • 我是初学者,请告诉我该怎么做?
    • 只需将应用程序上下文传递给 LazyImageLoadAdapter(context)
    • 那么您可以在 getSystemService(your activity context.WINDOW_SERVICE)) 处使用该上下文
    【解决方案2】:

    也许看看你一小时前发布的同一个问题已经得到回答?

    Is it possible to Adjust height of listview with respect to screen size when it is used with adapter

    【讨论】:

    • 但我仍然没有找到任何解决方案,
    【解决方案3】:

    试试这个。

            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int density = metrics.densityDpi;
    
            int width = 0, height = 0;
            metrics = new DisplayMetrics();        
             getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
             //height is actual screen height
             height = metrics.heightPixels;     
             width = metrics.widthPixels;
    
    
             CustomAdapter mAdapter;
             //pass h to your adapter class and it will gives you the height as per you need
             h=height/3;
             mAdapter = new CustomAdapter(this,ImageArrayList1, ImageArrayList2,h);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多