質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

6544閲覧

android studio Textviewの背景色/枠等の設定について

rockey_7625

総合スコア25

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2021/05/23 05:04

android studioにてアプリを開発中なのですが、Textviewの背景色の設定で悩んでおります。
Activity_main.xml側でviewの背景色/枠を設定しているのですが、コード側で”.setBackgroundColor(Color.*****);”を使うと色だけで無く枠の設定も変わってしまいます。

枠等の設定はそのままにして背景色のみ制御したいのですがどうすればいいのでしょうか?
色々とググりましたが調べきれず、申し訳ないのですがアドバイス頂けると助かります。

お手数ですがよろしくお願い致します。

MainActivity

1import androidx.appcompat.app.AppCompatActivity; 2import android.graphics.Color; 3import android.os.Bundle; 4import android.widget.Button; 5import android.widget.EditText; 6import android.widget.RadioButton; 7import android.widget.RadioGroup; 8import android.widget.TextView; 9 10public class MainActivity extends AppCompatActivity { 11 12 private int int_MinScale; 13 private int int_MaxScale; 14 private String str_MinScale; 15 private String str_MaxScale; 16 private EditText InMinScale; 17 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 RadioGroup rgroup = (RadioGroup)findViewById(R.id.radioGroup); 25 InMinScale = findViewById(R.id.input_min_scale); 26 EditText InMaxsSale = findViewById(R.id.input_max_scale); 27 EditText AdcPoint = findViewById(R.id.ADC_point); 28 EditText CurrPoint = findViewById(R.id.Current_point); 29 TextView Adcp_result = findViewById(R.id.ADC_point_result); 30 TextView Currp_result = findViewById(R.id.Cur_point_result); 31 Button calc_btn = findViewById(R.id.calculate_button); 32 RadioButton radioButton = findViewById(R.id.radioButton); 33 RadioButton radioButton2 = findViewById(R.id.radioButton2); 34 35 //ラジオボタンが選択されてなかったらスケールは入力できないようにする 36 if (radioButton.isChecked() == false && radioButton2.isChecked() == false) { 37 InMinScale.setEnabled(false); 38      //以下でsetBackgroundColorを使うとxml側の設定が無視されてしまう。 39 InMinScale.setBackgroundColor(Color.DKGRAY); 40 41 } 42 43 //以下、ラジオボタンで計算モードを選択 44 rgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 45 @Override 46 public void onCheckedChanged(RadioGroup radioGroup, int i) { 47 48 49 //以下計算モードは”ADC Value” 50 if (i == R.id.radioButton) { 51 boolean rdobtn_chk = radioButton.isChecked(); 52 fullScale(rdobtn_chk); 53 54 **ここは作成中**** 55 56 57 } else { 58 Adcp_result.setText("not in"); 59           **ここは作成中**** 60 61 } 62 63 } 64 } 65 }); 66 67 } 68 69 //以下、フルスケール入力関数 70 private void fullScale(boolean rdobtn_chk) { 71 InMinScale.setEnabled(true); 72 InMinScale.setBackgroundColor(Color.rgb(222,210,191)); 73 74 } 75 76}

activity_main.xml

1 2 <TextView 3 android:id="@+id/textView3" 4 android:layout_width="380dp" 5 android:layout_height="23dp" 6 android:layout_marginTop="8dp" 7 android:fontFamily="sans-serif-light" 8 android:text="@string/main_title" 9 android:textAlignment="center" 10 android:textColor="@color/white" 11 android:textSize="18sp" 12 android:textStyle="bold|italic" 13 android:typeface="monospace" 14 app:layout_constraintEnd_toEndOf="@+id/imageView3" 15 app:layout_constraintStart_toStartOf="@+id/imageView3" 16 app:layout_constraintTop_toTopOf="parent" /> 17 18 <RadioGroup 19 android:id="@+id/radioGroup" 20 android:layout_width="350dp" 21 android:layout_height="wrap_content" 22 android:layout_marginTop="16dp" 23 android:background="@drawable/frame_style2" 24 android:orientation="horizontal" 25 app:layout_constraintEnd_toEndOf="parent" 26 app:layout_constraintStart_toStartOf="parent" 27 app:layout_constraintTop_toTopOf="@+id/imageView2"> 28 29 <RadioButton 30 android:id="@+id/radioButton" 31 android:layout_width="139dp" 32 android:layout_height="wrap_content" 33 android:layout_weight="0.35" 34 android:text="@string/adc_value" /> 35 36 <RadioButton 37 android:id="@+id/radioButton2" 38 android:layout_width="139dp" 39 android:layout_height="wrap_content" 40 android:layout_weight="1" 41 android:text="@string/current_value_of_4_20ma" /> 42 43 </RadioGroup> 44 45 <TextView 46 android:id="@+id/calc_mode" 47 android:layout_width="350dp" 48 android:layout_height="17.5dp" 49 android:gravity="bottom" 50 android:text="@string/calculation_mode_select_pls_select_either_the_calculate_mode" 51 android:textColor="@android:color/black" 52 android:textSize="11sp" 53 app:layout_constraintBottom_toTopOf="@+id/radioGroup" 54 app:layout_constraintStart_toStartOf="@+id/radioGroup" /> 55 56 <ImageView 57 android:id="@+id/imageView" 58 android:layout_width="381dp" 59 android:layout_height="215dp" 60 android:layout_marginTop="32dp" 61 android:background="@drawable/frame_style3" 62 android:contentDescription="@string/todo" 63 app:layout_constraintBottom_toBottomOf="parent" 64 app:layout_constraintEnd_toEndOf="parent" 65 app:layout_constraintHorizontal_bias="0.466" 66 app:layout_constraintStart_toStartOf="@+id/imageView2" 67 app:layout_constraintTop_toTopOf="@+id/imageView2" 68 app:layout_constraintVertical_bias="0.652" /> 69 70 <EditText 71 android:id="@+id/input_min_scale" 72 android:layout_width="92dp" 73 android:layout_height="38dp" 74 android:layout_marginStart="4dp" 75 android:layout_marginBottom="160dp" 76 android:autofillHints="" 77 android:background="@drawable/frame_style2" 78 android:ems="10" 79 android:hint="@string/pls_input_mim" 80 android:inputType="textMultiLine" 81 android:lines="3" 82 android:textSize="11sp" 83 app:layout_constraintBottom_toBottomOf="@+id/imageView" 84 app:layout_constraintStart_toStartOf="@+id/radioGroup" />

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2021/05/24 04:17

drawable/frame_style2 がどんなものかによりますが、例えば EditText を別の ViewGroup の中に入れて、そっちで枠を指定するとか…。
rockey_7625

2021/05/24 10:59

drawable/frame_style2の中身は以下です。 <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="8dp"/> <solid android:color="`color@ivory"/> <stroke android:width="1.5dp" android:color="@color/black"/> </shape> コード側ではbackgourndColorのみ指定ですので枠の設定等はxml側の設定が反映されると思っていたのですがそうはならず。。。。。 コード側で枠等の設定も行う必要があるのでしょうか?
guest

回答1

0

ベストアンサー

Android の View には独立した枠線という概念はなく、あくまでも背景の一部という扱いになります。また、背景色を設定すると、それは枠線のない塗りつぶしの背景という意味になります。枠線を保ったまま背景色を指定するには、そのような drawable を作成する必要があります。

MainActivity.java

java

1 InMinScale.setBackgroundResource(R.drawable.frame_style2_gray);

res/drawable/frame_style2_gray.xml

xml

1<shape xmlns:android="http://schemas.android.com/apk/res/android"> 2 <corners android:radius="8dp"/> 3 <solid android:color="#FFFFF0"/> 4 5 <stroke 6 android:width="1.5dp" 7 android:color="@color/black"/> 8 9 <solid 10 android:color="@color/gray" /> 11 12</shape>

投稿2021/05/24 14:29

hoshi-takanori

総合スコア7895

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

rockey_7625

2021/05/25 13:05

アドバイス頂き誠にありがとうございました。 先ほど別のdrawableを作製し試してみたらうまくいきました。 たすかりました!!!
rockey_7625

2021/05/25 13:07

アドバイス頂き誠にありがとうございました。 たすかりました!!!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問