Androidのボタンの属性layout_weightの値をJavaコードから動的に設定するにはどうすればよいですか?
この例は、Androidのボタンの属性layout_weightの値をJavaコードから動的に設定する方法を示しています。
ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。
ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center" android:id="@+id/linearLayout" android:padding="4dp" tools:context=".MainActivity"> </LinearLayout>
ステップ3 −次のコードをsrc / MainActivity.java
に追加しますimport androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params1.weight = 0.3f;
Button button1 = new Button(this);
button1.setText("Button 1");
button1.setLayoutParams(params1);
linearLayout.addView(button1);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params2.weight = 0.7f;
Button button2 = new Button(this);
button2.setText("Button 2");
button2.setLayoutParams(params2);
linearLayout.addView(button2);
}
} ステップ4 −次のコードをandroidManifest.xmlに追加します
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
アイコン。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-
-
JavaでマージンをJButtonに設定するにはどうすればよいですか?
JButton AbstractButtonのサブクラスです また、JavaSwingアプリケーションにプラットフォームに依存しないボタンを追加するために使用できます。 JButonはActionListenerを生成できます ボタンが押されたときまたはクリックされたときのインターフェースは、 MouseListenerを生成することもできます およびKeyListener インターフェイス。 setMargin()を使用して、JButtonにマージンを設定できます。 JButtonクラスのメソッドとパスInsets(int top、int left、int bottom、int rig
-
Outlook for Android アプリでメールを設定する方法
Outlook は、何十年にもわたって最も人気のあるメール管理ソリューションの 1 つであり、世界中の複数の個人や企業によって使用されています。コンピューターで電子メールにアクセスするために使用する主要なツールかもしれませんが、スマートフォンについて話すときは確かに同じではありません.しかし、Android スマートフォンで Outlook を構成するのは非常に簡単で、数分で完了します。どのクライアントよりも優れた方法で電子メールを整理するだけでなく、スパマーや犯罪者があなたに到達するのを困難にします.したがって、読者がAndroidでOutlookをセットアップする方法を学ぶことは価値があり