GVKun编程网logo

android – 以编程方式设置组件的宽度和高度(在android应用程序设计中,组件大小的单位一般采用)

13

最近很多小伙伴都在问android–以编程方式设置组件的宽度和高度和在android应用程序设计中,组件大小的单位一般采用这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Andro

最近很多小伙伴都在问android – 以编程方式设置组件的宽度和高度在android应用程序设计中,组件大小的单位一般采用这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Android Kotlin 以编程方式设置 FrameLayout 的高度不起作用、android – 以编程方式获取布局的高度和宽度、android – 以编程方式设置Locale、android – 以编程方式设置TTS的语言?等相关知识,下面开始了哦!

本文目录一览:

android – 以编程方式设置组件的宽度和高度(在android应用程序设计中,组件大小的单位一般采用)

android – 以编程方式设置组件的宽度和高度(在android应用程序设计中,组件大小的单位一般采用)

在 Android 2.3.3中开发的应用程序

我正在开发计算器应用程序.

问题1 :::
我有大约16个按钮.有没有一种方法可以在没有它的情况下使用循环(或)来设置所有按钮的宽度和高度.我希望所有的按钮都是统一的.

问题2 :::
你怎么看待这种做法?是好是坏?请解释原因?
假设我每行有4个按钮.如果我以编程方式获得屏幕的宽度和高度,然后除以(宽度/ 4)并为每个按钮添加边距,然后分别设置按钮的宽度(宽度/ 4边距),这将以某种方式解决显示问题在不同尺寸的屏幕?

解决方法

根据设备宽度和高度同等地提供视图宽度或高度的最佳方法是使用权重属性,举个例子,我们有一个线性布局,有四个宽度相等的按钮:

<LinearLayout android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal">
     <Button android:layout_width="0dip"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="button1"/>
     <Button android:layout_width="0dip"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="button2"/>
     <Button android:layout_width="0dip"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="button3"/>
     <Button android:layout_width="0dip"
         android:layout_weight="1"
         android:layout_height="wrap_content"
         android:text="button4"/>
</LinearLayout>

但是,如果您仍希望在运行时提供布局宽度和高度,则可以使用setLayoutParams(new LayoutParams(100,100));方法.

Android Kotlin 以编程方式设置 FrameLayout 的高度不起作用

Android Kotlin 以编程方式设置 FrameLayout 的高度不起作用

如何解决Android Kotlin 以编程方式设置 FrameLayout 的高度不起作用?

我正在尝试:

        fragment_container.layoutParams.height = 100
        fragment_container.requestLayout()

        val params = fragment_container.layoutParams
        params.height = 100
        fragment_container.layoutParams = params
        fragment_container.requestLayout()

两者都不起作用,这个fragment_container(它是FrameLayout)的高度仍然和layout.xml中声明的一样

如果需要,这是布局:

<?xml version="1.0" encoding="utf-8"?>
<pl.jawegiel.exchangerates.ClickableMotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto"
    android:id="@+id/motion_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    motion:layoutDescription="@xml/main_activity_scene">

    <ImageView
        android:id="@+id/iv_header"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@mipmap/exchange_rates"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <LinearLayout
        android:id="@+id/ll_toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/teal_700"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="8dp"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toBottomOf="@+id/iv_header">

        <RelativeLayout
            android:id="@+id/rl_inside_toolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:textSize="20sp"
                android:text/>

            <TextView
                android:id="@+id/toolbar_subtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/toolbar_title"
                android:textSize="12sp"
                android:text/>
        </RelativeLayout>
    </LinearLayout>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="400dp"/>

    <TextView
        android:id="@+id/about"
        android:layout_width="wrap_content"
        android:layout_height="?attr/actionBarSize"
        android:gravity="center_vertical"
        android:text="@string/about"
        android:layout_marginEnd="10dp"
        android:textSize="18sp"
        android:visibility="visible"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintTop_toTopOf="parent" />
</pl.jawegiel.exchangerates.ClickableMotionLayout>

你还需要知道什么?还有什么事情你需要知道?我想我已经添加了您需要的所有内容,但以防万一。提前致谢!

解决方法

在@femi mcpd 的更正之后,它几乎可以工作了,但还剩下一件事。当我将您的代码放入此方法中时:

private fun initToolbarListener() {
    ll_toolbar.addOnLayoutChangeListener { view,_,_ ->
        llHeaderY =  view.y

        val frameLayout = findViewById<FrameLayout>(R.id.fragment_container)
        frameLayout.layoutParams.height = 400
    }

它不起作用,但我真的很想,但是如果我将它直接放在 onCreate 中,它会起作用。为什么?

,

终于全部解决了。我需要用RelativeLayout包装fragment_contaner,并像这样修改这个方法:

private fun initToolbarListener() {
    ll_toolbar.addOnLayoutChangeListener { view,_ ->
        llToolbarY = view.y.toInt()

        fragment_container.postDelayed({
            val params = fragment_container.layoutParams as RelativeLayout.LayoutParams
            params.height = getHeightOfScreenWithoutBars() - llToolbarY - ll_toolbar.height
            fragment_container.layoutParams = params
        },100)
    }
}

无论如何,再次感谢您!我接受这个问题是为了你的承诺:) 亲切的问候

android – 以编程方式获取布局的高度和宽度

android – 以编程方式获取布局的高度和宽度

我设计了一个布局,其中LinearLayout有2个子项LinearLayout和FrameLayout,并且在每个子项中我放置了不同的视图.

我只是想测量FrameLayout的高度和宽度,以便我可以达到我的目的.

在我正在使用的程序中

int height,width;

FrameLayout fr=(FrameLayout)findviewbyid(R.id.fr);
height=fr.getHeight();
width=fr.getWidth();

两者都返回0

即使我尝试使用以下代码
int height,width;

FrameLayout fr=(FrameLayout)findviewbyid(R.id.fr);
height=fr.getMeasuredHeight();
width=fr.getMeasuredWidth();

返回相同的值0

最后我尝试使用以下代码,
int height,width;

FrameLayout fr=(FrameLayout)findviewbyid(R.id.fr);
height=fr.getgetLayoutParams().height();
width=fr.getLayoutParams().width;

返回-2和-1

我希望解决方案以编程方式获取任何布局的高度和宽度?

解决方法:

视图本身,它有自己的生命周期,基本如下:

>附上
>测量
>布局
>画画

因此,根据您何时尝试获得宽度/高度,您可能看不到您期望看到的内容,例如,如果您在onCreate期间执行此操作,则另一方面,该视图可能甚至不会被测量如果你在按钮的onClick方法中这样做,很可能到目前为止该视图已被附加,测量,布局和绘制,因此,您将看到预期值,您应该实现ViewTreeObserver以确保您获得适当时刻的价值观.

LinearLayout layout = (LinearLayout)findViewById(R.id.YOUD VIEW ID);
ViewTreeObserver vto = layout.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                this.layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } 
        int width  = layout.getMeasuredWidth();
        int height = layout.getMeasuredHeight(); 

    } 
});

android – 以编程方式设置Locale

android – 以编程方式设置Locale

我的应用程序支持3种(很快4种)语言.由于几个语言环境非常相似,我想让用户选择在我的应用程序中更改语言环境,例如意大利人可能更喜欢西班牙语而不是英语.

有没有办法让用户在可用于应用程序的语言环境中进行选择,然后更改使用的语言环境?我不认为为每个Activity设置语言环境是一个问题,因为它是在基类中执行的简单任务.

解决方法:

对于仍在寻找此答案的人,由于从API 24弃用了configuration.locale,您现在可以使用:

configuration.setLocale(locale);

考虑到此方法的minSkdVersion是API 17.

完整示例代码:

@SuppressWarnings("deprecation")
private void setLocale(Locale locale){
    SharedPrefUtils.saveLocale(locale); // optional - Helper method to save the selected language to SharedPreferences in case you might need to attach to activity context (you will need to code this)
    Resources resources = getResources();
    Configuration configuration = resources.getConfiguration();
    displayMetrics displayMetrics = resources.getdisplayMetrics();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
        configuration.setLocale(locale);
    } else{
        configuration.locale=locale;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
        getApplicationContext().createConfigurationContext(configuration);
    } else {
        resources.updateConfiguration(configuration,displayMetrics);
    }
}

不要忘记,如果使用正在运行的Activity更改语言环境,则需要重新启动它才能使更改生效.

编辑2018年5月11日

从@ CookieMonster的帖子开始,您可能无法在较高的API版本中保持区域设置更改.如果是这样,请将以下代码添加到Base Activity,以便在每次创建Activity时更新上下文区域设置:

@Override
protected void attachBaseContext(Context base) {
     super.attachBaseContext(updateBaseContextLocale(base));
}

private Context updateBaseContextLocale(Context context) {
    String language = SharedPrefUtils.getSavedLanguage(); // Helper method to get saved language from SharedPreferences
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return updateResourcesLocale(context, locale);
    }

    return updateResourcesLocaleLegacy(context, locale);
}

@TargetApi(Build.VERSION_CODES.N)
private Context updateResourcesLocale(Context context, Locale locale) {
    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);
    return context.createConfigurationContext(configuration);
}

@SuppressWarnings("deprecation")
private Context updateResourcesLocaleLegacy(Context context, Locale locale) {
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;
    resources.updateConfiguration(configuration, resources.getdisplayMetrics());
    return context;
}

如果您使用此功能,请不要忘记在使用setLocate(语言环境)设置语言环境时将语言保存到SharedPreferences

android – 以编程方式设置TTS的语言?

android – 以编程方式设置TTS的语言?

我已经编写了一个小型的Android Demo来使用不同语言的TTS.我有两个按钮,西班牙语和英语的布局.按下按钮会触发所选语言的发声.

但是,我无法更改语言(setLanguage(Locale locale)).我可以手动完成,使用手机设置并将TTS语言更改为美国,英国,意大利,德国等,但我的代码似乎不起作用.你能告诉我问题出在哪里吗?

谢谢!!

package com.ignacio.SpeakAPP;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import java.util.Locale;

public class SpeakAPPActivity extends Activity implements OnInitListener {
private static final String TAG = "TextToSpeechDemo";
private TextToSpeech mTts;
public boolean Passer = false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

/** Handle the action of the English Button **/
public boolean talkNowEN(View v)
{

    mTts = new TextToSpeech (this, this);
    return Passer = false;
}

/** Handle the action of the Spanish Button **/
public boolean talkNowES(View v)
{
    mTts = new TextToSpeech (this, this);   
    return Passer = true;
}

/** TTS **/
public void onInit (int status){

    if (status ==TextToSpeech.SUCCESS){

        if(Passer==false){
            //If English Button was activated
            //Initialize speech to text, set language to english and send utterance
            mTts.setLanguage(Locale.US);
            mTts.speak("How may I help you?", TextToSpeech.QUEUE_FLUSH, null);  
        }else{
            //If Spanish Button was activated
            //Initialize speech to text, check if spanish is available, set locale to spanish and send utterance

            Locale loc = new Locale ("es", "ES");
            mTts.setLanguage(loc);
            if (result2==TextToSpeech.LANG_MISSING_DATA||result2==TextToSpeech.LANG_NOT_SUPPORTED){
                Log.e(TAG, "Language is not available");
            }else {
                mTts.speak("Como puedo ayudarte?", TextToSpeech.QUEUE_FLUSH, null);
            }

        }

    }else {
        Log.e(TAG, "Could not initialize TextToSpeech");
    }

}


@Override
protected void onDestroy(){
    super.onDestroy();
    mTts.shutdown();
} 

}

解决方法:

从https://web.archive.org/web/20120505124037/http://developer.android.com/resources/articles/tts.html开始,你可能想试试这个:

Locale loc = new Locale ("spa", "ESP");

看起来很奇怪,但这就是他们所引用的(不像人们期望的那样).

我们今天的关于android – 以编程方式设置组件的宽度和高度在android应用程序设计中,组件大小的单位一般采用的分享就到这里,谢谢您的阅读,如果想了解更多关于Android Kotlin 以编程方式设置 FrameLayout 的高度不起作用、android – 以编程方式获取布局的高度和宽度、android – 以编程方式设置Locale、android – 以编程方式设置TTS的语言?的相关信息,可以在本站进行搜索。

本文标签: