Created
January 31, 2018 07:36
-
-
Save xu1718191411/d70ab34a837c616d13e2e2ce42c36864 to your computer and use it in GitHub Desktop.
AndroidカスタマイズのLinearLayout + XMLから属性(attribute)を取得して表示させる ref: https://qiita.com/xu1718191411/items/4c1a7c322ef3c3db8908
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyLinerLayout extends LinearLayout { | |
private LayoutInflater mLayoutInflater; | |
private Resources mResources; | |
public MyLinerLayout(Context context) { | |
super(context); | |
} | |
public MyLinerLayout(Context context, @Nullable AttributeSet attrs) { | |
super(context, attrs); | |
initialize(attrs,0); | |
} | |
public MyLinerLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
initialize(attrs,defStyleAttr); | |
} | |
private void initialize(AttributeSet attrs, int defStyleAttr){ | |
int r = 255; | |
int g = 255; | |
int b = 255; | |
if(attrs != null){ | |
String packageName = "http://www.mrway.net/syoui"; | |
r = attrs.getAttributeIntValue(packageName,"r",255); | |
g = attrs.getAttributeIntValue(packageName,"g",255); | |
b = attrs.getAttributeIntValue(packageName,"b",255); | |
} | |
mResources = getContext().getResources(); | |
mLayoutInflater = LayoutInflater.from(getContext()); | |
View view = mLayoutInflater.inflate(R.layout.my_liner_layout, this); | |
((TextView) view.findViewById(R.id.color)).setText(r+":"+g+":"+b); | |
int backgroundColor = Color.argb(255,r,g,b); | |
((LinearLayout) view.findViewById(R.id.colorLayout)).setBackgroundColor(backgroundColor); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout | |
android:id="@+id/colorLayout" | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:id="@+id/color" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:text="white"/> | |
</LinearLayout> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class foundation_activity_get_attribute_from_xml extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_foundation_get_attribute_from_xml); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.ConstraintLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.example.syoui.imagetab.foundation.activity.foundation_activity_get_attribute_from_xml"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<com.example.syoui.imagetab.foundation.activity.MyLinerLayout | |
xmlns:color="http://www.mrway.net/syoui" | |
color:r="238" | |
color:g="34" | |
color:b="12" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1"> | |
</com.example.syoui.imagetab.foundation.activity.MyLinerLayout> | |
<com.example.syoui.imagetab.foundation.activity.MyLinerLayout | |
xmlns:color="http://www.mrway.net/syoui" | |
color:r="239" | |
color:g="95" | |
color:b="107" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1"> | |
</com.example.syoui.imagetab.foundation.activity.MyLinerLayout> | |
<com.example.syoui.imagetab.foundation.activity.MyLinerLayout | |
xmlns:color="http://www.mrway.net/syoui" | |
color:r="248" | |
color:g="186" | |
color:b="0" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
android:layout_weight="1"> | |
</com.example.syoui.imagetab.foundation.activity.MyLinerLayout> | |
</LinearLayout> | |
</android.support.constraint.ConstraintLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment