попробовать это:
MainActivity Код:
общественный класс MainActivity расширяет активность {
EditText etInput;
LinearLayout resultLayout;
Button btnResult;
ViewTreeObserver mVto;
int widthOfLine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etInput = (EditText) findViewById(R.id.etInput);
btnResult = (Button) findViewById(R.id.btnShowRes);
btnResult.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String temp = etInput.getText().toString().trim();
if(temp.contains("/")){
System.out.println("inside if");
String[] numArr = temp.split("/");
Log.d("dj", "num1: & num2: "+numArr[0]+" "+numArr[1]);
showResults(numArr[0], numArr[1]);
}
else Toast.makeText(getBaseContext(), "Pattern invalid", Toast.LENGTH_SHORT).show();
}
});
resultLayout = (LinearLayout) findViewById(R.id.showResultLay);
}
protected void showResults(String numerator, String denominator) {
final LinearLayout linChild = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
linChild.setGravity(Gravity.CENTER);
linChild.setLayoutParams(childParams);
linChild.setOrientation(LinearLayout.VERTICAL);
TextView tvNumerator = new TextView(getBaseContext());
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tvNumerator.setLayoutParams(tvParams);
linChild.addView(tvNumerator);
tvNumerator.setText(numerator);
tvNumerator.setTextColor(Color.BLACK);
tvNumerator.setTextSize(25);
final View dividerView = new View(getBaseContext());
dividerView.setBackgroundColor(Color.RED);
linChild.addView(dividerView);
final TextView tvDenominator = new TextView(getBaseContext());
LinearLayout.LayoutParams tv1Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tvDenominator.setLayoutParams(tv1Params);
linChild.addView(tvDenominator);
tvDenominator.setTextColor(Color.BLACK);
tvDenominator.setText(denominator);
tvDenominator.setTextSize(25);
mVto = linChild.getViewTreeObserver();
mVto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
widthOfLine = tvDenominator.getWidth();
LinearLayout.LayoutParams viewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 5);
viewParams.width = widthOfLine;
dividerView.setLayoutParams(viewParams);
}
});
resultLayout.addView(linChild);
}
}
файл макета:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="dj.numeratordenominator.main.MainActivity" >
<EditText
android:id="@+id/etInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="textNoSuggestions" >
<requestFocus />
</EditText>
<LinearLayout
android:id="@+id/showResultLay"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btnShowRes"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" >
</LinearLayout>
<Button
android:id="@+id/btnShowRes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etInput"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
Работает так, как ожидалось. @ Суммарно – DJphy
он не работает, как и ожидалось. когда я пишу 1/2 + 3, тогда он пишет _____ 2 + 3 –
он не работает, как и ожидалось. когда я пишу 1/2 + 3 то написать - _____ 2 +- , и это также дает мне ошибку на линии, подобную этой LinearLayout.LayoutParams (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) ; , и я использую его с LinearLayout.LayoutParams (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); –