RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-318053

Ruben Kubalyan's questions

Martin Hope
Ruben Kubalyan
Asked: 2020-04-03 16:05:21 +0000 UTC

为什么横向 UI 绘制正确而纵向绘制错误?

  • 0

为什么横向 UI 绘制正确而纵向绘制错误,而这两个选项都是基于几行 java 代码自动绘制的(两种模式相同)?

这是 IDE 的屏幕预览

在此处输入图像描述

这是预览信息

在此处输入图像描述

这是自动调整 UI 元素大小的代码

 @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

        //размеры выбранной версии превью, на основе которого я собрал нужный UI для программы 
        int testedWidth = 1200;
        int testedHeight = 1920;

        //получаем размер экрана устройства/дисплея
        metrics = Resources.getSystem().getDisplayMetrics();
        displayWidth = metrics.widthPixels;
        displayHeight = metrics.heightPixels;

        widthMultiplier = ((double) displayWidth) / ((double) testedWidth);
        heightMultiplier = ((double) displayHeight) / ((double) testedHeight);

        ...

        for (TextView textView : textViews) {
            viewWidth = textView.getWidth();
            viewHeight = textView.getHeight();

            RelativeLayout.LayoutParams lp = copyLp((RelativeLayout.LayoutParams) textView.getLayoutParams());
            lp.width = (int) (viewWidth * widthMultiplier);
            lp.height = (int) (viewHeight * heightMultiplier);

            // Setting the parameters on the TextView
            textView.setLayoutParams(lp);
        }
        ...

这是设备的结果(我做了一个屏幕截图)

景观

在此处输入图像描述

肖像

在此处输入图像描述

问题的本质:如果结果正确,在纵向时,ViewGroup 的宽度(包含循环形式的元素)应该等于设备屏幕的宽度,即 ViewGroup 的左侧应该触摸屏幕的左侧,以及 ViewGroup 的右侧 - 到右侧屏幕。但结果如你所见,LSES-LSV之间和ESEU-PSV之间有空位,说明ViewGroup的宽度不等于设备的宽度。

澄清:LSEU是“设备屏幕的左侧”,LSV是“视图组的左侧”,PSEU-PSV分别是“设备屏幕的右侧”-“视图的右侧”。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/pokemon_background"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/relative_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="@dimen/tvWidthSmallBoard"
            android:layout_height="@dimen/tvHeightSmallBoard"
            android:background="@drawable/search_icon_for_searchboard_cell"
            android:gravity="center"
            android:text="@string/click_me"
            android:textColor="@android:color/black"
            android:visibility="visible" />
        ...
    </RelativeLayout>   
    ...
</RelativeLayout>

拉伸不正确。理论上,它应该被拉伸,使得给定 ViewGroup 的宽度与 displayWidth 的大小(运行程序的设备的宽度)一致。

正如你从上面的代码中知道的

int testedWidth = 1200;
int testedHeight = 1920;

以及程序为设备屏幕找到的屏幕尺寸

宽度=1080 高度=2131

奇怪的是,横向 - 没有问题,宽度按预期拉伸,但出于某种原因,纵向,而不是充分拉伸,程序决定在右侧绘制小的所有循环,但是为什么会发生这种情况,如果包含循环的 ViewGroup 没有硬边框,但是 wrap_content 很重要吗?

我自己找不到这种情况的合乎逻辑的解释。

添加。信息:

xml中的size值不是传给“dp”而是传给“px”的。为什么?这是因为减少了尺寸测量和计算过程中的麻烦。我最近做了一个带有“dp”值的程序,问题的数量让我很生气,我不得不切换到“px”。对于动态调整 UI 元素的大小,这绝对是正确的解决方案,所以请不要建议去“dp”。

这是复制粘贴dimens.xml(只有必要的行):

<?xml version="1.0" encoding="utf-8"?>
<resources>
...
    <dimen name="tvWidthSmallBoard">400px</dimen>
    <dimen name="tvHeightSmallBoard">200px</dimen>
...
</resources>

好东西我记起来了。忘记指定这个方法了,程序中使用的

private RelativeLayout.LayoutParams copyLp(RelativeLayout.LayoutParams source){
        return new RelativeLayout.LayoutParams(source);
}
android
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-04-01 18:53:06 +0000 UTC

如何找出多少px等于dp中传递给xml的值?

  • 0

在这里,我编写了一个程序,它可以根据程序运行的设备自动调整 View 元素的大小。

在某些情况下,在自动模式下正确调整这些元素的大小存在问题,并且由于我最初将所有值传递给 dp 而不是 px,因此出现了不必要的问题。

这是一个例子。预览在 width:height (1080:1920) 模式下完成;420dpi

如果我给出的值是 100dp,我如何找出多少像素等于一个 View 元素的宽度?

或者如何找出整个屏幕的宽度和高度是多少dp?

自然,420dpi 并不意味着宽度和高度都等于 420dp。这甚至在预览中可见,无需任何计算。

当然,我在 startandroid 中阅读了此信息

为了避免在不同分辨率下出现这种情况,建议使用dp(和sp)。它可以定义为一个可伸缩的 px。屏幕密度负责可扩展性的程度。这是系统用来计算 dp 值的因素。目前这个系数有5个值:

  • 低(ldpi)= 0.75

  • 中 (mdpi) = 1

  • 电视 (tvdpi) = 1.33

  • 高 (hdpi) = 1.5

  • 超高 (xhdpi) = 2

那些。当屏幕设置为 mdpi 模式时,则 1 dp = 1 px。那些。100 dp 宽的按钮看起来与 100 px 宽的按钮相同。

例如,如果我们有一个低分辨率的屏幕,则使用 ldpi 模式。在这种情况下,1 dp = 0.75 像素。那些。100 dp 宽的按钮看起来与 75 px 宽的按钮相同。

如果我们有高分辨率屏幕,则使用 hdpi 或 xhdpi 模式。1dp = 1.5 像素或 2 像素。100 dp 宽的按钮看起来与 150 像素或 200 像素宽的按钮相同。

那些。不同的分辨率使用不同的密度模式,这些模式允许应用程序缩放和查看,如果不相同,那么至少在所有屏幕上都相似。

但是我怎么能理解如何进行正确的计算呢?例如,我如何知道在我的手机启动过程中预览和结果是在上述选项的哪个模式下显示的?

android
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-03-31 22:13:58 +0000 UTC

为什么在更改相关布局中一组视图元素的宽度和高度时,所有这些视图都忘记了它们的位置?

  • 1

https://youtu.be/3p3x4TCwh14(这是以编程方式调整视图大小前后结果的视频截图)

下面是我更改 View 元素的宽度和高度的代码:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    //получаем размер экрана устройства/дисплея
    metrics = Resources.getSystem().getDisplayMetrics();
    displayWidth = metrics.widthPixels;
    displayHeight = metrics.heightPixels;

    widthMultiplier =  (double) displayWidth / (double) testedWidth;
    heightMultiplier = displayHeight/(double) testedHeight;

    Log.i("userTest2020",String.valueOf("display width = " + displayWidth+"\n"+"display height = "+displayHeight));

    if(hasFocus) {
        for (TextView textView:textViews) {
            viewWidth = textView.getWidth();
            viewHeight = textView.getHeight();

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    (int)(viewWidth*widthMultiplier),
                    (int)(viewHeight*heightMultiplier));

            // Setting the parameters on the TextView
            textView.setLayoutParams(lp);
            //break;
        }

        for (ImageView imageView: imageViews) {
            viewWidth = imageView.getWidth();
            viewHeight = imageView.getHeight();

            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    (int)(viewWidth*widthMultiplier),
                    (int)(viewHeight*heightMultiplier));

            // Setting the parameters on the ImageView
            imageView.setLayoutParams(lp);
            //break;
        }

        /**/viewWidth = tv10.getWidth();
        viewHeight = tv10.getHeight();

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                (int)(viewWidth*widthMultiplier),
                (int)(viewHeight*heightMultiplier));

        // Setting the parameters on the ImageView
        tv10.setLayoutParams(lp);

        viewWidth = tv11.getWidth();
        viewHeight = tv11.getHeight();

        lp = new RelativeLayout.LayoutParams(
                (int)(viewWidth*widthMultiplier),
                (int)(viewHeight*heightMultiplier));

        // Setting the parameters on the ImageView
        tv11.setLayoutParams(lp);

        viewWidth = b1.getWidth();
        viewHeight = b1.getHeight();

        lp = new RelativeLayout.LayoutParams(
                (int)(viewWidth*widthMultiplier),
                (int)(viewHeight*heightMultiplier));

        // Setting the parameters on the ImageView
        b1.setLayoutParams(lp);

        viewWidth = b2.getWidth();
        viewHeight = b2.getHeight();

        FrameLayout.LayoutParams lp2 = new FrameLayout.LayoutParams(
                (int)(viewWidth*widthMultiplier),
                (int)(viewHeight*heightMultiplier));

        // Setting the parameters on the ImageView
        b2.setLayoutParams(lp2);

        viewWidth = b3.getWidth();
        viewHeight = b3.getHeight();

        lp2 = new FrameLayout.LayoutParams(
                (int)(viewWidth*widthMultiplier),
                (int)(viewHeight*heightMultiplier));

        // Setting the parameters on the ImageView
        b3.setLayoutParams(lp2);/**/
    }
}

有一种假设是,这是因为进行新的渲染意味着创建新的 UI,而 xml 中指定的所有内容都失去了相关性,所有内容都必须通过代码从头开始创建。但是,既然程序仍然通过在xml中设置的相应id来识别每个View,那么逻辑是不是被破坏了?

对应xml文件的内容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/pokemon_background"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/relative_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/search_icon_for_searchboard_cell"
            android:gravity="center"
            android:text="@string/click_me"
            android:textColor="@android:color/black"
            android:visibility="visible"/>

        <TextView
            android:id="@+id/tv2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_toEndOf="@+id/tv1"
            android:background="@drawable/search_icon_for_searchboard_cell"
            android:gravity="center"
            android:text="@string/click_me"
            android:textColor="@android:color/black"
            android:visibility="visible" />

        <TextView
            android:id="@+id/tv3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_toEndOf="@+id/tv2"
            android:background="@drawable/search_icon_for_searchboard_cell"
            android:gravity="center"
            android:text="@string/click_me"
            android:textColor="@android:color/black"
            android:visibility="visible" />

        <TextView
            android:id="@+id/tv4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/tv1"
            android:background="@drawable/search_icon_for_searchboard_cell"
            android:gravity="center"
            android:text="@string/click_me"
            android:textColor="@android:color/black"
            android:visibility="visible" />

        <TextView
            android:id="@+id/tv5"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/tv2"
            android:layout_toEndOf="@+id/tv4"
            android:background="@drawable/search_icon_for_searchboard_cell"
            android:gravity="center"
            android:text="@string/click_me"
            android:textColor="@android:color/black"
            android:visibility="visible" />

        <TextView
            android:id="@+id/tv6"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/tv3"
            android:layout_toEndOf="@+id/tv5"
            android:background="@drawable/search_icon_for_searchboard_cell"
            android:gravity="center"
            android:text="@string/click_me"
            android:textColor="@android:color/black"
            android:visibility="visible" />

        <TextView
            android:id="@+id/tv7"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@id/tv4"
            android:background="@drawable/search_icon_for_searchboard_cell"
            android:gravity="center"
            android:text="@string/click_me"
            android:textColor="@android:color/black"
            android:visibility="visible" />

        <TextView
            android:id="@+id/tv8"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/tv5"
            android:layout_toEndOf="@+id/tv7"
            android:background="@drawable/search_icon_for_searchboard_cell"
            android:gravity="center"
            android:text="@string/click_me"
            android:textColor="@android:color/black"
            android:visibility="visible" />

        <TextView
            android:id="@+id/tv9"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/tv6"
            android:layout_toEndOf="@+id/tv8"
            android:background="@drawable/search_icon_for_searchboard_cell"
            android:gravity="center"
            android:text="@string/click_me"
            android:textColor="@android:color/black"
            android:visibility="visible" />

        <ImageView
            android:id="@+id/iv1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_above="@+id/iv4"
            android:layout_toStartOf="@+id/iv5"
            android:alpha="0.4"
            android:background="@color/colorPrimary"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/iv2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_above="@+id/iv5"
            android:layout_toEndOf="@id/iv4"
            android:alpha="0.4"
            android:background="@color/colorPrimary"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/iv3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_above="@+id/iv5"
            android:layout_toEndOf="@+id/iv5"
            android:alpha="0.4"
            android:background="@color/colorPrimary"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/iv4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerInParent="true"
            android:layout_toStartOf="@+id/iv5"
            android:alpha="0.4"
            android:background="@color/colorPrimary"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/iv5"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:alpha="1"
            android:background="@drawable/pika_pika"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/iv6"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@id/iv3"
            android:layout_toEndOf="@+id/iv5"
            android:alpha="0.4"
            android:background="@color/colorPrimary"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/iv7"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@id/iv4"
            android:layout_toStartOf="@+id/iv5"
            android:alpha="0.4"
            android:background="@color/colorPrimary"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/iv8"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@id/iv5"
            android:layout_toStartOf="@+id/iv6"
            android:alpha="0.4"
            android:background="@color/colorPrimary"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/iv9"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@id/iv6"
            android:layout_toEndOf="@+id/iv5"
            android:alpha="0.4"
            android:background="@color/colorPrimary"
            android:visibility="invisible" />
    </RelativeLayout>

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:alpha="0.3"
            android:background="@android:color/white"
            android:gravity="center"
            android:text=" "
            android:visibility="visible" />

        <TextView
            android:id="@+id/tv10"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:text=" "
            android:textColor="@android:color/black"
            android:visibility="visible" />


        <Button
            android:id="@+id/b1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerHorizontal="true"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:text="get resolution info"
            android:visibility="visible" />


        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true">

            <Button
                android:id="@+id/b2"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginStart="5dp"
                android:background="@android:color/transparent"
                android:gravity="center"
                android:text="new round"
                android:visibility="visible" />

            <ImageView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginStart="5dp"
                android:alpha="0.3"
                android:background="@color/colorGreen"
                android:gravity="center"
                android:text="new round"
                android:visibility="visible" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_below="@id/relative_layout"
            android:layout_centerHorizontal="true">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:alpha="0.3"
                android:background="@color/colorRed"
                android:gravity="center"
                android:text="Try the bigger board"
                android:visibility="visible" />

            <Button
                android:id="@+id/b3"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@android:color/transparent"
                android:gravity="center"
                android:text="Try the bigger board"
                android:textColor="@color/colorYellow"
                android:visibility="visible" />
        </FrameLayout>

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerHorizontal="true"
            android:alpha="0.3"
            android:background="@android:color/white"
            android:gravity="center"
            android:text="get resolution info"
            android:visibility="visible" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentEnd="true"
            android:layout_marginStart="5dp"
            android:alpha="0.3"
            android:background="@android:color/white"
            android:gravity="center"
            android:visibility="visible" />

        <TextView
            android:id="@+id/tv11"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentEnd="true"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:text="You're score is 0"
            android:textColor="@android:color/black"
            android:visibility="visible" />
</RelativeLayout>
android
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-03-30 11:54:35 +0000 UTC

无法以编程方式从 View 获取宽度和高度

  • 0

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/textView"
        android:layout_width="400px" 
        android:layout_height="200px"
        android:background="@color/colorAccent"
        android:text="textView" />

</LinearLayout>

主要活动

public class MainActivity extends AppCompatActivity {
    TextView textView;
    LinearLayout.LayoutParams p;

    DisplayMetrics metrics;

    int displayWidth;
    int displayHeight;

    int viewWidth;
    int viewHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);

        //получаем размеры интересующего нашего View
        viewWidth = textView.getWidth();
        viewHeight = textView.getHeight();

        //выводим данные о View (проверим, насколько точно будут выведены значения (сравним со значениями, которые мы передали в xml файле)
        //для этого давайте использовать Log.
        Log.i("userTest2020", String.valueOf("view width = "+viewWidth+"\n"+"view height = "+viewHeight));
    }

    @Override
    protected void onResume() {
        super.onResume();

        //получаем размеры интересующего нашего View
        //viewWidth = textView.getWidth();
        //viewHeight = textView.getHeight();

        //выводим данные о View (проверим, насколько точно будут выведены значения (сравним со значениями, которые мы передали в xml файле)
        //для этого давайте использовать Log.
        Log.i("userTest2020", String.valueOf("view width = "+viewWidth+"\n"+"view height = "+viewHeight));
    }
}

结果

2020-03-30 07:48:51.994 28754-28754/com.bignerdranch.testresoulutionchanges I/userTest2020: view width = 0
    view height = 0
2020-03-30 07:48:52.004 28754-28754/com.bignerdranch.testresoulutionchanges I/userTest2020: view width = 0
    view height = 0

问题是什么?

注意:

起初我尝试使用“dp”在xml中传递宽度和高度值。

然后改为“px”。

另外,除了onCreate之外,我还在onResume中使用了它,以查看这个问题是否是由于View xml根本没有时间实现。

但显然——也许不是时候。到目前为止,我没有其他线索。

这就是为什么我在这里问我的问题。

结果还是一样。

android
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-03-01 17:07:39 +0000 UTC

为什么在不同情况下 id 之前存在和不存在 + 符号并不总是产生逻辑结果?

  • 1

我正在看一本书,偶然发现了这个信息。(在屏幕下方显示为红色)

在此处输入图像描述

好吧,乍一看,一切似乎都是透明的:“当为所需的 View 添加唯一 id 时,必须使用 + 符号,而当使用已经创建的 id 时,必须移除 + 符号。”

但我决定尝试一下,这就是我遇到的:

1)它工作正常(好吧,这段代码来自我的一个长期编写的应用程序),虽然书中说你需要删除 + 符号(这将是合乎逻辑的)。

在此处输入图像描述

2) 从这两行中删除了 + 符号。

结果:它似乎再次正常工作(并且比以前的版本更合乎逻辑,基于书中的上述陈述),在预览的意义上给出了相同的结果。

在此处输入图像描述

但我决定走得更远:我取并注释掉了第一个视图所指的一个视图(我用它进行实验)

在此处输入图像描述

这就是我在这个案例中重复前两个实验时最终发生的事情:

1) 如果像前面的相应选项一样,我们将 + 符号留在原处(在不删除它的意义上),那么我们得到以下结果:

在此处输入图像描述

这很奇怪吗?但第二点更奇怪。

2) 如果像前面的相应选项一样,删除了 + 符号,那么通常会出现令人惊讶的结果:

在此处输入图像描述

没有错误,没有警告???怎么会这样???

好吧,我焦躁不安,我进一步挖掘并偶然发现了其他东西。

由于在之前的实验中考虑了 tv1 (tv4, tv5) 模型,现在我将考虑另一个模型来更全面地了解正在发生的事情,即 tv7 (tv4, tv5) 模型。

所以,结果:

1) 注释掉 tv4,从某种意义上说,我们删除了注释模式,并再次执行最近为 tv1 模型(tv4、tv5)所做的第一个实验。结果:

在此处输入图像描述

没有警告,没有错误。根据书中的上述陈述,直观但不合逻辑。

2)如果您删除 + 符号,让 tv4 处于活动模式,您会得到以下信息:

在此处输入图像描述

同样,没有警告,没有错误。根据书中的上述陈述,这次很直观但很合乎逻辑。

现在是冠号。

我们再次评论 tv4 并重复实验(与模型 tv1 (tv4, tv5) 期间相同)

1) 如果你留下 + 符号,你会得到以下信息:

在此处输入图像描述

可以看到,在模型 tv1 (tv4, tv5) 期间的结果与之前的实验相同

2)但是如果你去掉+号,那么这次实验给出了完全不同的结果,我注意到这次的结果在逻辑和直观层面上都是可以理解的:

在此处输入图像描述

android
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-01-07 06:02:24 +0000 UTC

确定手机、平板电脑的屏幕尺寸

  • 0

在启动过程中,确定手机的屏幕尺寸和相应的显示元素。

事实是,正如我所注意到的,不断使用相同的“匹配父级”和“包装内容”并不总是有用的,而且它们通常会迫使程序员以这样一种方式组合设计,即在调试 GPU 覆盖时,我们将看到一种深红色,在有关编译设计大大降低程序性能的信息的意义上。如果您使用 px、dp、... 而不是“包装内容”和“匹配父级”,那么您很容易发现在不同设备上以不同方式显示程序屏幕的问题。

这实际上是我提出问题的原因,我在上面描述过。

android
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-12-08 20:04:02 +0000 UTC

如果代码中有这么多红线,像 LinearLayout 这样的类是如何工作的?

  • 0

代码如何在这种情况下工作?

在此处输入图像描述

出于兴趣,我尝试断开与 Internet 的连接。令我惊讶的是,代码再次起作用。我的意思是,该程序是在我的 Android 手机上启动的。

还想知道如何研究这些代码以了解它们的工作原理?

附加功能:顺便说一下,我是 Android 编程新手。

android
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-11-06 18:16:40 +0000 UTC

枚举问题。为什么一定要写“;”?

  • 2
public enum Class1000{
    ;   // почему это обязательно писать? 
    public static void main(String[] args) {

    }
}

为什么要写“;” 在调用实验性 psvm 之前或在声明字段或覆盖已实现接口的方法等期间的枚举主体中......?

众所周知,在此符号之前,编译器将所有书面名称视为枚举。

java
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-11-05 07:51:28 +0000 UTC

关于 try-with-resources 构造中抑制异常的问题

  • 0

这是 Oracle-Certified-Professiona-Java-SE-8-Programmer-Exam-1Z0-809 一书中的引述

在 try-with-resources 语句中,可能会抛出多个异常;例如,一个在 try 块中,一个在 catch 块中,另一个在 finally 块中。但是,只能捕获一个异常,因此其他异常将被列为抑制异常。从给定的异常对象中,您可以使用 getSuppressed() 方法来获取被抑制的异常列表。

我在实践中尝试过:

System.out.println("Type an integer in the console: ");
        try(Scanner consoleScanner = new Scanner(System.in)) {
            System.out.println("You typed the integer value: " + consoleScanner.nextInt());
            int kil = 8/0;
        } catch(Exception e) {
// catch all other exceptions here ...
            System.out.println("Error: Encountered an exception and could not read an integer from the console... ");
            System.out.println("Exiting the program - restart and try the program again!");
        }finally {
            int kil = 8/0;
        }

结果一点也不奇怪:

Type an integer in the console: 
5
You typed the integer value: 5
Error: Encountered an exception and could not read an integer from the console... 
Exiting the program - restart and try the program again!
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at package3.MethodReference.main(MethodReference.java:183)

没有抑制发生。还是正在发生?那么如何理解异常输出到控制台呢?还是我误会了什么?

java
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-11-02 07:00:29 +0000 UTC

流 API(anyMatch()、allMatch()、noneMatch())

  • 2

与 anyMatch() 方法在流为空时返回 false 不同,allMatch() 和 noneMatch() 方法在流为空时返回 true!

问题:为什么在一个空流的情况下返回 false,而在其他情况下返回 true?

请求:帮助我理解逻辑,为什么一切都是这样而不是相反。

java
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-10-28 05:32:40 +0000 UTC

StringBuilder 类的 trimToSize() 方法,它是干什么用的?

  • 0
    StringBuilder stringBuilder = new StringBuilder("polodik");
    stringBuilder.setLength(50);
    System.out.println(stringBuilder.capacity());
    stringBuilder.trimToSize();
    System.out.println(stringBuilder.capacity());
    System.out.println(stringBuilder);

结果控制台:

50
50
polodik 

关于 trimToSize() 方法

尝试减少用于字符序列的存储空间。如果缓冲区大于保存其当前字符序列所需的大小,则可以调整其大小以提高空间效率。调用此方法可能(但不是必须)影响后续调用 {@link #capacity()} 方法返回的值。

所以问题是:它做了什么以及如何正确使用 trimToSize 方法,以便至少看到对控制台结果的一些影响?

java
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-10-25 14:41:20 +0000 UTC

关于 String.format() 方法的问题

  • 0
String s =" polakshfUodfaasp[eorfjcnnxbxaxxakfaaheryo";
String[] s2 = {"5000","8000"};
    System.out.println(String.format(s+"%s", s2));

控制台输出:

 polakshfUodfaasp[eorfjcnnxbxaxxakfaaheryo5000

问题:为什么只显示5000?

注意:如果突然发现这个问题是重复的,那么只需将链接粘贴在评论中,不要投反对票。如果链接的内容给出了详尽的答案,那么我将删除我的问题。

java
  • 2 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-10-21 21:53:24 +0000 UTC

关于Iterable接口的foreach()方法操作的问题

  • 1

有这样的代码

 List<String> strings = Arrays.asList("eeny", "meeny", "miny", "mo");
        strings.forEach(string -> System.out.println(string));

目前尚不清楚它是如何工作的,除了我知道 lambda 可以工作,因为 Consumer 接口是一个函数式接口。

但其余的非常混乱。也许整个问题是我难以理解位于 Iterable 接口中的以下表达式

default void forEach(Consumer<? super T> action) {/*содержание тела метода 
                                                  мне понятна, поэтому не 
                                                  скопировал тут*/}

请求:一步一步写出这段代码的算法会很有用。

注意:如果突然发现这个问题是重复的,那么只需将链接粘贴在评论中,不要投反对票。如果链接的内容给出了详尽的答案,那么我将删除我的问题。

java
  • 2 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-10-20 22:48:55 +0000 UTC

Arrays.asList() 是我看不懂的 Java OCP 书中的一个有趣的语句

  • 2

Oracle-Certified-Professona-Java-SE-8-Programmer-Exam-1Z0-809.pdf 书说这样的声明:

java.util.Arrays 类有一个名为 asList() 的有用方法,它返回一个固定大小的列表。这是返回的 List 对象的一个​​有趣方面:您不能添加或删除元素,但可以修改 asList() 方法返回的对象!此外,您通过 List 所做的修改会反映在原始数组中

从某种意义上说,据我了解,从合成表到 Arrays.asList 没有办法添加或删除元素。

但是为什么不允许添加新元素呢?我想知道为什么 List 通常允许您添加元素,但在这种情况下却不允许。这难道不是导致细微错误的原因吗?

Double [] temperatureArray = {31.1, 30.0, 32.5, 34.9, 33.7, 27.8};
List<Double> temperatureList = Arrays.asList(temperatureArray);
temperatureList.add(5D); 

Exception in thread "main" java.lang.UnsupportedOperationException
    at java.base/java.util.AbstractList.add(AbstractList.java:153)
    at java.base/java.util.AbstractList.add(AbstractList.java:111)
    at package3.UtilitiesTest.main(UtilitiesTest.java:44)

注意:如果突然发现这个问题是重复的,那么只需将链接粘贴在评论中,不要投反对票。如果链接的内容给出了详尽的答案,那么我将删除我的问题。

java
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-10-18 18:48:52 +0000 UTC

通用方法,编译器“善良”的原因是什么?

  • 2
class Utilities {       
    public static <T> void flo(T k, T p){}
}

public class UtilitiesTest {
    public static void main(String []args) {
        Utilities.flo(5,"5"); //почему компилятор не выдает ошибку?
    }
}

T 作为单个泛型参数假定相同的类型,对吗?所以在此基础上编译器不会发誓“等一下,你设置了两种不同的类型,但你需要传输相同类型的数据”

但是在这种情况下,编译器为什么不发誓似乎是合乎逻辑的。

class Utilities {
    public static <T1,T2> void flo(T1 k, T2 p){}
}

public class UtilitiesTest {
    public static void main(String []args) {
        Utilities.flo(5,"5"); //а в данном случае понятно почему компилятор спокоен
   }
} 

但是如果在这个例子中

class Utilities {
    public static <T> void fill(List<T> list, T val) {
        for(int i = 0; i < list.size(); i++)
            list.set(i, val);
    }
}

public class UtilitiesTest {
    public static void main(String []args) {
        List<Integer> intList = new ArrayList<>();
        intList.add(10);
        intList.add(20);
        System.out.println("The original list is: " + intList);
        Utilities.fill(intList, 5);
        System.out.println("The list after calling Utilities.fill() is: " + intList);
    }
}

进行实验,然后由于某种原因编译器记住了它的职责(在某种意义上,“发誓”的职责)

class Utilities {
    public static <T> void fill(List<T> list, T val) {
        for(int i = 0; i < list.size(); i++)
            list.set(i, val);
    }
}

public class UtilitiesTest {
    public static void main(String []args) {
        List<Integer> intList = new ArrayList<>();
        intList.add(10);
        intList.add(20);
        System.out.println("The original list is: " + intList);
        Utilities.fill(intList, "5"); //хоть тут компилятор ругается!
        System.out.println("The list after calling Utilities.fill() is: " + intList);
    }
}

注意:如果突然发现这个问题是重复的,那么只需将链接粘贴在评论中,不要投反对票。如果链接的内容给出了详尽的答案,那么我将删除我的问题。

java
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-10-16 17:26:10 +0000 UTC

为什么不可能在枚举本身内创建枚举对象?

  • 1
enum  EnumClass{
    ENUM_CLASS1, ENUM_CLASS2, ENUM_CLASS3;
    EnumClass enumClass = new EnumClass(); //не получается
}

从逻辑上讲,这没有任何意义。也很清楚为什么不可能在枚举之外创建实例,因为枚举构造函数是私有的。但是在枚举中,为什么不能创建实例?可以说,私有表明对象不能在其他地方创建,而是在枚举本身内部?现在已经不清楚了。

注意:如果突然发现这个问题是重复的,那么只需将链接粘贴在评论中,不要投反对票。如果链接的内容给出了详尽的答案,那么我将删除我的问题。

java
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-10-15 18:36:15 +0000 UTC

调用新的匿名类方法

  • 0
interface Lopzik{
    void petrol();
}

  public class Test{
    public static void main(String[] args) {
        Lopzik lopzik = new Lopzik(){
            @Override
            public void petrol() {
                System.out.println("ouch");
            }

            public String toString() {
                return "poncho";
            }
            public String kopo(){return "last";} //можно как то вызвать данный метод тут? (номер 2)
        };
        lopzik.petrol();
        // номер 2
    }
}

如果这个方法在 Lopzik{} 中不存在,但是在定义匿名类的过程中被注入,你怎么能在 main 方法中调用 kopo() 方法呢?还是只能在匿名类的主体中使用它?

注意:如果突然发现这个问题是重复的,那么只需将链接粘贴在评论中,不要投反对票。如果链接的内容给出了详尽的答案,那么我将删除我的问题。

java
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-10-15 08:39:01 +0000 UTC

为什么Java不允许创建本地接口?

  • 3

奇怪的是不能创建本地接口,但是可以创建抽象的本地类。

问题:为什么不允许本地创建接口,而允许创建抽象类?

了解和理解这种奇怪的原因非常有趣。另一件事,如果抽象类也被禁止在本地创建,那么至少在“假设”层面上会更容易理解

注意:如果突然发现这个问题是重复的,那么只需将链接粘贴在评论中,不要投反对票。如果链接的内容给出了详尽的答案,那么我将删除我的问题。

java
  • 2 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-10-13 11:46:28 +0000 UTC

开关设计的奇怪选择性

  • 2

有这样的代码,一切正常。

  public static void main(String[] args) {
        final int num = 0;

        switch (num) {
            default:
                System.out.println("default");
            case num: //нет проблем
                System.out.println("case1");
            case 10 * 2 - 10:
                System.out.println("case2");
                break;
        }
    }

但这样的即兴创作是值得的

 public static void main(String[] args) {
        final int num;
        num = 0;

        switch (num) {
            default:
                System.out.println("default");
            case num:  // Constant expression required
                System.out.println("case1");
            case 10 * 2 - 10:
                System.out.println("case2");
                break;
        }
    }

瞧……有一个错误。

有趣的是,如果 final 字段在声明时没有初始化,那么它就不再是 final 了。

我要澄清一下:我尝试在一次后期初始化之后再次初始化,看看是否可以通过某种魔法多次初始化 final 字段。最后,不,一切都很好。您只能初始化一次。

那么,交换机在什么基础上不将传递的值识别为最终值?

java
  • 1 个回答
  • 10 Views
Martin Hope
Ruben Kubalyan
Asked: 2020-10-08 01:24:02 +0000 UTC

加载主类时发生 LinkageError (java.lang.UnsupportedClassVersionError) (Java)

  • 1

安装了 jdk 9,然后更改为 jdk 12。 在此处输入图像描述

并在 Intellij 中更改了项目结构选项

在此处输入图像描述

当尝试打开使用 Intellij jar 创建的文件时,它会给出以下结果

在此处输入图像描述

我以为还好,看来一切都是因为我忘记重启系统了。重新启动了,问题还是一样。

顺便说一句,jar 也被删除并再次创建,效果也为零。

问:怎么理解?


2019 年 10 月 12 日更新问题

这就是我注意到的...

在此处输入图像描述

java
  • 2 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5